iOS 开发之手势
手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断。用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:
- UITapGestureRecognizer
- UIPinchGestureRecognizer
- UIRotationGestureRecognizer
- UISwipeGestureRecognizer
- UIPanGestureRecognizer
- UILongPressGestureRecognizer
上面的手势对应的操作是:
- Tap(点一下)
- Pinch(二指往內或往外拨动,平时经常用到的缩放)
- Rotation(旋转)
- Swipe(滑动,快速移动)
- Pan (拖移,慢速移动)
- LongPress(长按)
具体代码如下
#import "ViewController.h"
@interface ViewController ()
{
NSArray *colorlist;
UIImageView *imageView ;
UIActivityIndicatorView *bb;
UITextField *textField;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [ UIColor grayColor ] ;
imageView = [[ UIImageView alloc ] initWithFrame:CGRectMake(0, 0, 40, 40) ];
imageView.image = [ UIImage imageNamed:@"1.jpg" ] ;
[ self.view addSubview:imageView ] ;
textField = [[ UITextField alloc ]initWithFrame:CGRectMake(100, 100, 200, 40)];
textField.backgroundColor = [ UIColor whiteColor ];
textField.placeholder = @"请输入密码";
textField.textAlignment = NSTextAlignmentCenter ;
textField.secureTextEntry = YES ;
textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation ;
textField.keyboardAppearance = UIKeyboardAppearanceDark ;
textField.clearButtonMode = UITextFieldViewModeAlways ;
textField.returnKeyType = UIReturnKeyGoogle ;
UIView *viewww = [[ UIView alloc ]initWithFrame:CGRectMake(0, 0, 40, 40)];
viewww.backgroundColor = [ UIColor redColor ];
textField.leftView = viewww ;
textField.leftViewMode = UITextFieldViewModeAlways ;
[self.view addSubview:textField];
// 如果不设置背景颜色 有可能不响应 手势
// 手势如果不响应
// 1、看是否添加到 imageView 如果是 把 userInteractionEnabled 设置成 YES
// 2、查看是否 设置了背景 颜色
// 3、手势冲突 设置手势的优先级 (截获触发 事件、手势)设置优先级
// 五大手势 : 点击(长按)、拖拽、旋转、捏合、轻扫 (都属于手势 继承自 UIGestureRecognizer 父类 )
// 五打手势之一 : 点击
UITapGestureRecognizer *tap = [ [ UITapGestureRecognizer alloc ] initWithTarget:self action:@selector(tapAction:) ] ;
// 设置点击 手指的 个数
tap.numberOfTouchesRequired = 1 ;
// 设置点击次数
tap.numberOfTapsRequired = 1 ;
// 把 手势 添加到 某个 视图 上
[self.view addGestureRecognizer:tap ] ;
// 五打手势之一 : 长按
UILongPressGestureRecognizer *longPress = [[ UILongPressGestureRecognizer alloc ]initWithTarget:self action:@selector(longPress:)];
// 最小 按下 的持续时间
longPress.minimumPressDuration = 1 ;
// longPress.numberOfTouchesRequired = 2 ;
[self.view addGestureRecognizer:longPress ];
// 五打手势之二 : 拖拽
UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer alloc ] initWithTarget:self action:@selector(pan:)];
// 设置最小的手指 个数
// pan.minimumNumberOfTouches = 1 ;
// 设置最多的手指个数
// pan.maximumNumberOfTouches = 4 ;
[ self.view addGestureRecognizer:pan ] ;
// 五打手势之三 : 轻扫
UISwipeGestureRecognizer *swipe = [[ UISwipeGestureRecognizer alloc ] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft ;
// 处理 拖拽 和 两个手势 的冲突
// 执行完 swipe 手势 之后 再 执行 pan 手势
[ pan requireGestureRecognizerToFail:swipe ] ;
[ self.view addGestureRecognizer:swipe ] ;
// 五打手势之四 : 捏合
UIPinchGestureRecognizer *pinch = [ [ UIPinchGestureRecognizer alloc ] initWithTarget:self action:@selector(pinch:)];
[self.view addGestureRecognizer:pinch ] ;
// 五打手势之五 : 旋转
UIRotationGestureRecognizer *rotation = [[ UIRotationGestureRecognizer alloc ]initWithTarget:self action:@selector(rotation:)];
[ self.view addGestureRecognizer:rotation ] ;
}
// 点击手势触发的方法
- (void)tapAction:( UITapGestureRecognizer * )sender
{
[ textField resignFirstResponder ];
bb = [[ UIActivityIndicatorView alloc ]init];
bb.color = [ UIColor redColor ] ;
bb.center = self.view.center ;
[self.view addSubview:bb];
[bb startAnimating];
// self.view.backgroundColor = [ UIColor redColor ] ;
// 点击 恢复 视图的 初始 形态
// imageView.transform = CGAffineTransformIdentity ;
// 获得 手势 的 触摸点(中心点)
CGPoint tapLoc = [ sender locationInView:self.view ] ;
[ UIView animateWithDuration:3 animations:^{
// 点击的位置 设置成图片的中心点
imageView.center = tapLoc ;
} ] ;
NSLog( @"%f%f",tapLoc.x,tapLoc.y ) ;
}
// 长按手势触发的方法
- (void)longPress:( UILongPressGestureRecognizer * )sender
{
CGPoint tapLoc = [ sender locationInView:self.view ] ;
[ UIView animateWithDuration:2 animations:^{
// 点击的位置 设置成图片的中心点
imageView.center = tapLoc ;
} ] ;
}
// 拖拽 手势的 触发 的 方法
- (void)pan:(UIPanGestureRecognizer *)sender
{
[ bb stopAnimating ] ;
CGPoint tapLoc = [ sender locationInView:self.view ] ;
imageView.center = tapLoc ;
}
//轻扫 手势 的 触发 的 方法
- (void)swipe:(UISwipeGestureRecognizer * )sender
{
self.view.frame = CGRectMake(CGRectGetWidth([ UIScreen mainScreen ].bounds), 0, CGRectGetWidth([ UIScreen mainScreen ].bounds), CGRectGetHeight([ UIScreen mainScreen ].bounds)) ;
[ UIView animateWithDuration:0.5 animations:
^{
self.view.frame = CGRectMake(0, 0, CGRectGetWidth([ UIScreen mainScreen ].bounds), CGRectGetHeight([ UIScreen mainScreen ].bounds));
}
] ;
}
// 捏合 手势 触发 的 方法
- (void) pinch:(UIPinchGestureRecognizer *)sender
{
[ textField resignFirstResponder ];
// 通过手势 得到的 变化比例 让 imageView 的形态发生 改变
imageView.transform = CGAffineTransformScale(imageView.transform, sender.scale, sender.scale) ;
}
// 旋转 手势 的 触发 的 方法
- (void)rotation:(UIRotationGestureRecognizer *)sender
{
CGPoint taploc = [sender locationInView:self.view ] ;
// 通过 旋转 手势 让图片 进行 相同 的 变形
imageView.transform = CGAffineTransformMakeRotation(sender.rotation) ;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end