- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 335, 627)];
imageView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:imageView];
//打开imageView的用户交互属性, 让他可以响应用户点击事件
imageView.userInteractionEnabled = YES;
[imageView release];
UIImage *image = [UIImage imageNamed:@"2.jpg"];
imageView.image = image;
// 手势的种类
// 1. 点击
// 2. 长按
// 3. 捏合
// 4. 轻扫
// 5. 拖拽
// 6. 旋转
// 7. 屏幕边缘拖拽
//1. 点击(tap)
// UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// //执行方法需要点击几次
// tap.numberOfTapsRequired = 2;
// //需要几根手指头点击
// tap.numberOfTouchesRequired = 2;
// //给view添加一个点击手势
// [imageView addGestureRecognizer:tap];
// [tap release];
//2. 长按
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// //判定为长按手势需要的时间, 默认0.5秒, 按多长时间算长按
// longPress.minimumPressDuration = 3;
// //在判定过程中, 允许用户手指移动的距离
// longPress.allowableMovement = 10;
// [imageView addGestureRecognizer:longPress];
// [longPress release];
//3, 捏合
// UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
// [imageView addGestureRecognizer:pinch];
// [pinch release];
//4. 轻扫(swipe)
// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// //设置检测的方向, 这个是向左
// swipe.direction = UISwipeGestureRecognizerDirectionLeft;
// [imageView addGestureRecognizer:swipe];
// [swipe release];
//5.拖拽
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
// [imageView addGestureRecognizer:pan];
// [pan release];
//6.旋转
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
// [imageView addGestureRecognizer:rotation];
// [rotation release];
//屏幕边缘拖拽
UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(aaa:
)];
[imageView addGestureRecognizer:screen];
[screen release];
}
//点击执行的方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"点击手势");
}
//长按执行的方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
//长按手势重写了:触摸开始. 触摸移动. 触摸结束
//手势的状态
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
}
}
//捏合的方法
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合");
//获得手势所在的view
UIView *view = pinch.view;
view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
//轻扫的方法
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"轻扫");
}
//
- (void)panAction:(UIPanGestureRecognizer *)pan
{
//获得手势所在的view
UIView *view = pan.view;
CGPoint p = [pan translationInView:view];
view.transform = CGAffineTransformTranslate(view.transform, p.x, p.y);
[pan setTranslation:CGPointZero inView:view];
NSLog(@"拖拽");
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rot
{
UIView *view = rot.view;
view.transform = CGAffineTransformRotate(view.transform, rot.rotation);
rot.rotation = 0;
NSLog(@"旋转");
}
- (void)aaa:( UIScreenEdgePanGestureRecognizer *)aaa
{
NSLog(@"屏幕边缘拖拽");
}