UI2_UIGesture

时间:2025-02-09 10:04:33
//
// ViewController.h
// UI2_UIGesture
//
// Created by zhangxueming on 15/7/9.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UIGestureRecognizerDelegate> @end
//
// ViewController.m
// UI2_UIGesture
//
// Created by zhangxueming on 15/7/9.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *names = @[@"blue",@"red",@"yellow"];
for (int i=0; i<3; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100*i, 180+100*i, 100, 100)];
imageView.image = [UIImage imageNamed:names[i]];
[self.view addSubview:imageView];
//打开用户交互使能
imageView.userInteractionEnabled = YES; //添加点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//设置点击次数
tap.numberOfTapsRequired = 1;
//设置手指个数
tap.numberOfTouchesRequired = 2;
//给imageView 添加手势
[imageView addGestureRecognizer:tap]; //添加长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
longPress.numberOfTapsRequired = 0;
//longPress.numberOfTouchesRequired = 2;
[imageView addGestureRecognizer:longPress]; //添加移动手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[imageView addGestureRecognizer:pan]; //捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[imageView addGestureRecognizer:pinch];
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[imageView addGestureRecognizer:rotation];
//轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[imageView addGestureRecognizer:swipe];
}
} - (void)tapGesture:(UITapGestureRecognizer *)tap
{
NSLog(@"图片被点击");
} - (void)longPressGesture:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"长按手势被触发");
} - (void)panGesture:(UIPanGestureRecognizer *)pan
{
NSLog(@"移动手势被触发");//5 10 15 5 5 5
UIView *view = pan.view;//5+5+5
//获取手势的偏移量
CGPoint px = [pan translationInView:self.view];
if(pan.state == UIGestureRecognizerStateBegan || pan.state == UIGestureRecognizerStateChanged)
{
//改变手势对应的view中心点坐标
pan.view.center = CGPointMake(view.center.x+px.x, view.center.y+px.y);
}
//设置偏移量为0;
[pan setTranslation:CGPointZero inView:self.view];
} - (void)pinchGesture:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合手势被触发");
if(pinch.scale == UIGestureRecognizerStateBegan || pinch.scale ==UIGestureRecognizerStateChanged)
{
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
}
//设置系数为1
pinch.scale = 1.0;
} - (void)rotationGesture:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"旋转手势被触发");
if (rotation.state == UIGestureRecognizerStateBegan || rotation.state == UIGestureRecognizerStateChanged) {
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
} rotation.rotation = 0.0;
} - (void)swipeGesture:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"轻扫手势被触发");
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

相关文章