ios UI控件的简单整理(1)
/**
匿名类目:可以声明方法和变量,属性为private(不允许在外部调用,且不能被继承
*//**
发送数据的委托方,接收数据的时代理发(即代理的反向传值)
委托方第一步:声明协议
委托方第二步:声明代理指针
委托方第三步:操作完成,告诉代理(即调用代理的方法)
代理第一步:遵守协议
代理第二步:成为代理
代理第三步:实现协议方法
*/// %zd %zi 表示NSInteger// %g 表示数字去掉尾零//代码块路径/Users/ms/Library/Developer/Xcode/UserData/CodeSnippets#pragma mark - Xcode快捷键Control + A:移动光标到行首
Control + E:移动光标到行末
Control + D:删除光标右边的字符
Control + K:删除本行
// %zd %zi 表示NSInteger// %g 表示数字去掉尾零//代码块路径/Users/ms/Library/Developer/Xcode/UserData/CodeSnippets#pragma mark - Xcode快捷键Control + A:移动光标到行首
Control + E:移动光标到行末
Control + D:删除光标右边的字符
Control + K:删除本行
Command + ->:移动到行尾#pragma mark - 本地化
// 找到这个仓库NSUserDefaults*ud = [NSUserDefaultsstandardUserDefaults];// 存放数据[ud setObject:@"hehe"forKey:@"a"];// 同步[ud synchronize];// 从ud里通过key查询存储的对象(可以存放NSString,NSNumber,数组,数组,NSDate,NSData)// 注:ud里不能存放自定义类型(放在容器里也不行)NSString*str = [ud objectForKey:@"a"];
#pragma mark - 通知NSDictionary*dic = [NSDictionarydictionaryWithObjectsAndKeys:@"111", @"aaa",nil];// 通过通知中心,发送一条通知,可以用第二个参数// 工程中所有监控fm90的对象都可以收到者个通知[[NSNotificationCenterdefaultCenter] postNotificationName:@"FM90"object:self.textView.textuserInfo:dic];// 在通知中心注册了一个观察者(self),当工程中任何地方发送了FM90这个通知,self都会触发listenUp这个方法[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(listenUp:) name:@"FM90"object:nil];// 如果通知中心的观察者回调里有参数,参数就是NSNotification- (void)listenUp:(NSNotification*)sender
{// 发送广播时带的两个参数// NSLog(@"%@ , %@", sender.name, sender.userInfo);}
- (void)dealloc
{// 当观察者dealloc的时候,需要移除观察者身份[[NSNotificationCenterdefaultCenter] removeObserver:selfname:@"FM90"object:nil];
}
#pragma mark - 获取程序文件相关目录// 获取APP Home 目录NSString* homeDirectory = NSHomeDirectory();// 获取 文件.app 目录NSString* appPath = [[NSBundlemainBundle] bundlePath];// 获取 Documents 目录NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);NSString* path = [paths objectAtIndex:0];// 获取 Library 目录paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask,YES);
path = [paths objectAtIndex:0];// 获取 Caches 目录paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);
path = [paths objectAtIndex:0];// 获取程序资源路径//path = [NSBundle mainBundle] pathForResource:(NSString *)ofType:(NSString *)// 加载资源//[[NSBundle mainBundle] loadNibNamed:@"string" owner:nil options:nil]
#pragma mark - 手势// 找到触摸事件UITouch *momo = [touches anyObject];// 判断触摸的图片是不是图片视图if([momo.viewisKindOfClass:[UIImageViewclass]]) {
[self.viewbringSubviewToFront:momo.view];// 记录鼠标的原始坐标(移动轨迹原始坐标)_tempPoint = [momo locationInView:self.view];
}// 鼠标点击次数momo.tapCount/**************六种基本手势******************/// 点击手势(松开鼠标时响应)UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(tapCR:)];// 将手势添加到view上[view addGestureRecognizer:tapGR];// 拖拽(移动,拖动,move)手势(这个手势和清扫手势冲突)UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:selfaction:@selector(panGR:)];// 缩放手势UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:selfaction:@selector(pinchGR:)];// 旋转手势UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:selfaction:@selector(rotationGR:)];// 长按手势UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(longPressGR:)];// 轻扫手势(和移动手势冲突)UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipeGR:)];// 设置轻扫方向swipeGR.direction= UISwipeGestureRecognizerDirectionUp;// 如果想响应多个方向的清扫需要创建多个清扫手势的对象UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipeGR:)];// 设置轻扫方向swipeLeft.direction= UISwipeGestureRecognizerDirectionLeft;// 轻扫- (void)swipeGR:(UISwipeGestureRecognizer *)swipeGR
{CGRectframe = swipeGR.view.frame;// 判断轻扫方向if(swipeGR.direction== UISwipeGestureRecognizerDirectionUp) {
frame.size.height+=10;
}// 按位与判断(用数字1进行按位与运算的结果,可以用按位与来判断是否相等)if(swipeGR.direction& UISwipeGestureRecognizerDirectionLeft) {
frame.size.height-=10;
}
}// 长按- (void)longPressGR:(UILongPressGestureRecognizer *)longPressGR
{// 判断手势状态if(longPressGR.state== UIGestureRecognizerStateBegan)
}// 旋转- (void)rotationGR:(UIRotationGestureRecognizer *)rotationGR
{// 让view旋转,后面一个参数就是旋转的角度rotationGR.view.transform= CGAffineTransformRotate(rotationGR.view.transform, rotationGR.rotation);// 每次旋转以后需要将记录还原rotationGR.rotation=0;
}// 缩放- (void)pinchGR:(UIPinchGestureRecognizer *)pinchGR
{// 让view缩放,后面2个参数,一个是x方向缩放的倍数,一个是y方向缩放的倍数pinchGR.view.transform= CGAffineTransformScale(pinchGR.view.transform, pinchGR.scale, pinchGR.scale);// 每次缩放后需要还原倍数的记录pinchGR.scale=1.0;
}// 移动- (void)panGR:(UIPanGestureRecognizer *)panGR
{// 移动手势对应的消息(x方向移动多少,y方向移动多少)CGPointpoint = [panGR translationInView:self.view];// 根据轨迹修改手势对应的viewpanGR.view.center= CGPointMake(panGR.view.center.x+ point.x, panGR.view.center.y+ point.y);// 每次移动以后需要将手势的轨迹清零[panGR setTranslation:CGPointMake(0,0) inView:self.view];
}// 点击- (void)tapCR:(UITapGestureRecognizer *)tagRG
{
[self.viewbringSubviewToFront:tagRG.view];
}