1.轻拍手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
[myView addGestureRecognizer:tap];
[tap release];
tap.numberOfTapsRequired = 2; // 设置点击次数(双击)
tap.numberOfTouchesRequired = 2; // 设置需要手指数
2.轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
[myView addGestureRecognizer:swipe];
[swipe release];
swipe.direction = UISwipeGestureRecognizerDirectionUp; // 设置轻扫方向
3.长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress)];
[myView addGestureRecognizer:longPress];
[longPress release];
longPress.minimumPressDuration = 2; // 设置长按时间
4.平移手势
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan)];
[myView addGestureRecognizer:pan];
[pan release];
- (void)panAction:(UIPanGestureRecognizer *)pan{
CGPointpoint = [pan translationInView:pan.view];
// transform是视图的一个重要属性我们的平移旋转缩放改变的都是视图的transform属性我们用何种方法去改变他取决于赋值符号右边调用的api
//我们之后学习的动画有很多API都是跟transform有关的
//平移手势在做这个动作时相当于实时地触发这个方法我们每一次的平移都会在上一次平移的基础上继续平移相当于每一次新的值都会再次变为老的值
pan.view.transform=CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
[pan setTranslation:CGPointZero inView:pan.view]; //重新置成0点
}
5.缩放手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch)];
[myView addGestureRecognizer:pinch];
[pinch release];
-(void)pinch:(UIPinchGestureRecognizer*)pinch{
pinch.view.transform=CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
6.旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation)];
[myView addGestureRecognizer:rotation];
[rotation release];
-(void)rotation:(UIRotationGestureRecognizer*)rotation{
//下一次的旋转都会根据上一次的旋转继续旋转
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation=0;
}
手势部分的笔记:
UIImageView是用来展示图片的 系统没有给我们提供addTarget action方法所以我们想让UIImageView可以点击需要给其加手势
手势一根分为七大种每一个手势的类都是继承于UIGestureRecognizer我们操作的时候是操作其子类(边界手势本文没有写)
一个手势只能添加到一个视图上如果添加到多个视图上只是最后添加的那个视图才会拥有这个手势
当我们想要操作ImageView时 需要将其交互打开(默认是关闭的) 交互打开的代码如下imageView.userInteractionEnabled = YES;