创建一个UIImageView类的对象
注意要把对象imageView的交互开了
// 创建一个imageView 添加手势用
UIImage *image = [UIImage imageNamed:@"Selected"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageView.image = image;
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
[imageView release];
创建好imageView后 把创建的手势对象添加到imageView上去:
tap – 轻拍
手势类 UIGestureRecognizer
这个类是个抽象类 其具体功能 交给子类去实现
抽象类就是省代码的类
// 初始化一个轻拍手势的对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 添加手势到视图上
[imageView addGestureRecognizer:tap];
// 释放
[tap release];
// 实现轻拍方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"你拍我了, 很轻");
UIImageView *imageView = (UIImageView *)tap.view;
imageView.image = [UIImage imageNamed:@"Highlighted"];
}
longPress – 长按
// 长按
// 添加手势步骤
// 1. 初始化手势 添加手势触发调用的方法
// 2. 把手势添加到视图上
// 3. 释放手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 设置长按时间
longPress.minimumPressDuration = 2.0;
[imageView addGestureRecognizer:longPress];
[longPress release];
// 实现长按手势
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 判断一下状态 长按 只需要出发一次方法
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按触发了多少次");
}
}
rotation – 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAciton:)];
[imageView addGestureRecognizer:rotation];
[rotation release];
// 实现旋转手势
- (void)rotationAciton:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"旋转...跳跃...");
// 形变属性 transform
// 参数一 要改变对应视图的形变属性
// 参数二 根据弧度去创建
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
// 每次转需要把旋转的角度 重置为0
// 因为要接替上一次的角度 开始旋转
rotation.rotation = 0;
}
pinch – 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAciton:)];
[imageView addGestureRecognizer:pinch];
[pinch release];
// 实现捏合收拾方法
- (void)pinchAciton:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合");
// 相册的捏合 跟这个手势 没什么关系
// 根据缩放的刻度(比例)改变形变属性
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
// 重置捏合的比例
pinch.scale = 1;
}
pan – 平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
[pan release];
// 实现平移方法
- (void)panAction:(UIPanGestureRecognizer *)pan
{
NSLog(@"平移");
// 获取平移的点(相对于要平移的视图)
CGPoint p = [pan translationInView:pan.view];
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
[pan setTranslation:CGPointMake(0, 0) inView:pan.view];
}
swipe – 清扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipe];
[swipe release];
// 实现轻扫方法
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"左扫");
}
screenEdgePan – 边缘扫
这个类继承于UIPanGestureRecognizer
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];
// 设置下从哪个边缘开始扫
screenEdgePan.edges = UIRectEdgeRight;
[imageView addGestureRecognizer:screenEdgePan];
[screenEdgePan release];
// 实现边缘扫
- (void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
NSLog(@"一定是 靠近屏幕边缘开始扫 才能触发");
}