ios中的手势

来源:互联网 发布:上海地铁客流量数据 编辑:程序博客网 时间:2024/06/05 00:08
手势很简单,前面是准备工作,重点在下面//  准备工作@interface RootViewController ()@property (nonatomic ,retain) UIImageView *amyImageView;@property (nonatomic, retain) UIImageView *bmyImageView;//记录开始的点@property (nonatomic, assign) CGPoint startPoint;@end@implementation RootViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view. //创建两个imageView    self.amyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 200, 200)];    self.amyImageView.backgroundColor = [UIColor orangeColor];    [self.view addSubview:self.amyImageView];    self.amyImageView.userInteractionEnabled = YES;    self.bmyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 375, 100)];    self.bmyImageView.backgroundColor = [UIColor orangeColor];    [self.view addSubview:self.bmyImageView];/ ********** 1.  轻拍 手势 以及详细的设置  **************//    一个 UITapGestureRecognizer 对象只能添加到一个view 上////    UITapGestureRecognizer  是个抽象类,主要是提供一些通用的属性和方法,一般我们不用抽象类来创建对象,而是它的子类们//    UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];////    将手势 添加到 imageV 上//    [self.amyImageView addGestureRecognizer:tapG];////    imageView  和 lable 的用户交互默认为关闭  需要打开//    self.amyImageView.userInteractionEnabled = YES;////    轻拍次数//    tapG.numberOfTapsRequired = 1;////    需要多少个手指//    tapG.numberOfTouchesRequired = 1;//    [tapG release];////    ***************//    UITapGestureRecognizer *tapG1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];//    //    将手势 添加到 imageV 上//    [self.bmyImageView addGestureRecognizer:tapG1];//    //    imageView  和 lable 的用户交互默认为关闭  需要打开//    self.bmyImageView.userInteractionEnabled = YES;//    //    轻拍次数//    tapG1.numberOfTapsRequired = 1;//    //    需要多少个手指//    tapG1.numberOfTouchesRequired = 1;//    [tapG1 release];////    self.amyImageView.tag = 1000;//    self.bmyImageView.tag = 10/ ********** 2.  长按 手势  **********************//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongAction:)];//    [self.amyImageView addGestureRecognizer:longPress];//    [longPress releas//********* 3.  旋转 手势  *************************//    UIRotationGestureRecognizer *rotaGR = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotaGRAction:)];//    [self.amyImageView addGestureRecognizer:rotaGR];//    [rotaGR release];/ ********** 4.  捏合 手势  *************************    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];    [self.amyImageView addGestureRecognizer:pinch];    [pinch release];}// 下面是:** 手势添加后执行的方法 ** //  4 . 捏合 的方法- (void)pinchAction:(UIPinchGestureRecognizer *)sender {    self.amyImageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);}//   3.  旋转方法//- (void)rotaGRAction:(UIRotationGestureRecognizer *)sender {////    transform 属性是view 的属性,是做图形变换的,也叫2d仿射变换,内部实现是用矩阵公式来计算的//    self.amyImageView.transform = CGAffineTransformMakeRotation(sender.rotation);////}////  2.  长按 手势动作      长按 移动view 的位置//- (void)LongAction:(UILongPressGestureRecognizer *)sender {//    NSLog(@"长按 手势");//    if (sender.state == UIGestureRecognizerStateBegan) {//        NSLog(@"长按 开始");//        //    获取 手指触摸屏幕的点//        self.startPoint = [sender locationInView:self.amyImageView];////        NSLog(@"point = %@", NSStringFromCGPoint(self.startPoint));////    } else if (sender.state == UIGestureRecognizerStateChanged) {//        NSLog(@"长按  中");//        //    获取 手指触摸屏幕的点//        CGPoint point = [sender locationInView:self.amyImageView];//        CGFloat dx = point.x - self.startPoint.x;//        CGFloat dy = point.y - self.startPoint.y;////        NSLog(@"point = %@", NSStringFromCGPoint(point));////        改变中心点的坐标    原来坐标 + 移动距离//        self.amyImageView.center = CGPointMake(self.amyImageView.center.x + dx, self.amyImageView.center.y + dy);//    } else if (sender.state == UIGestureRecognizerStateEnded) {////        NSLog(@"长按 结束");//    }//}////  1 . 轻拍手势 的动作//- (void)tapAction:(UITapGestureRecognizer *)tap {//////    *****  重要的属性 tap.view////    强转//    UIImageView *imageView = (UIImageView *)tap.view.tag;//    if (imageView.tag == 1000) {//         self.amyImageView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];//    } else {//        NSLog(@"aaaaaa");//    }//}
1 0