视图的单击,双击,捏合,扫动,旋转,长按等。

-(void)loadView
{
    [super loadView];
    self.view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 184, 200, 200)];
    imageView.image = [UIImage imageNamed:@"1.png"];
    //打开图片的交互(不打开不能手势操作)  ps:很重要的细节
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;
    imageView.tag = 100;
    [self.view addSubview:imageView];
    [imageView release];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                         initWithTarget:self
                                         action:@selector(handleTapAction:)];
    
    // 点击,双击
    singleTap.numberOfTapsRequired = 1;
    [imageView addGestureRecognizer:singleTap];
    
    
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]
                                         initWithTarget:self
                                        action:@selector(handleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:doubleTap];
    
    [singleTap requireGestureRecognizerToFail:doubleTap];
    
    UITapGestureRecognizer *threeTap = [[UITapGestureRecognizer alloc]
                                         initWithTarget:self
                                         action:@selector(handleTapAction:)];
    threeTap.numberOfTapsRequired = 3;
    [imageView addGestureRecognizer:threeTap];
    
    [doubleTap requireGestureRecognizerToFail:threeTap];
    
    
    [singleTap release];
    [doubleTap release];
    [threeTap release];
    
    
    // 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
    [imageView addGestureRecognizer:pinch];
    [pinch release];
    
    
    // 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationAction:)];
    [imageView addGestureRecognizer:rotation];
    [rotation release];
    
    // 横扫
    UISwipeGestureRecognizer *swipeR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAction:)];
    swipeR.numberOfTouchesRequired = 1;
    swipeR.direction = UISwipeGestureRecognizerDirectionRight;
    [imageView addGestureRecognizer:swipeR];
    [swipeR release];
    
    UISwipeGestureRecognizer *swipeL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAction:)];
    swipeL.numberOfTouchesRequired = 1;
    swipeL.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:swipeL];
    [swipeL release];
    
    // 长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressAction:)];
    [imageView addGestureRecognizer:longPress];
    [longPress release];
    
    // 拖动
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)];
    
    [imageView addGestureRecognizer:pan];
    [pan release];
}
ps:被系统封装了的手势方法
-(void)handleTapAction:(UITapGestureRecognizer *)sender
{
    if (sender.numberOfTapsRequired == 1)
    {
        NSLog(@"这是单击");
        // insert code here...
    }
    if (sender.numberOfTapsRequired == 2)
    {
        NSLog(@"这是双击");
        // insert code here...
    }
    if (sender.numberOfTapsRequired == 3)
    {
        NSLog(@"这是三击");
        // insert code here...
    }
    if (sender.numberOfTapsRequired == 4)
    {
        NSLog(@"这是四击");
        // insert code here...
    }
    
}

-(void)handlePinch:(UIPinchGestureRecognizer *)sender
{
    UIImageView *imageV = (UIImageView *)[self.view viewWithTag:100];
    CGRect rect = imageV.frame;
    
    rect.size.width = sender.scale*imageV.image.size.width;
    rect.size.height = sender.scale*imageV.image.size.height;
    imageV.frame = rect;
    imageV.center = self.view.center;
    //    sender.scale
}

-(void)handleRotationAction:(UIRotationGestureRecognizer *)sender
{
    UIImageView *imageV = (UIImageView *)[self.view viewWithTag:100];
    imageV.transform = CGAffineTransformRotate(imageV.transform, sender.rotation);
    sender.rotation = 0;
//    if (sender.state == UIGestureRecognizerStateEnded)
//    {
//        sender.rotation = 0;
//        return;
//    }
    // imageV.transform 参考的那个点(当前点) sender.rotation 旋转一定的角度
    // 第一个参数是当前倾斜度,第二个参数是需要转动得倾斜度
    //CGAffineTransform transform = CGAffineTransformRotate(imageV.transform, sender.rotation);
   
    //imageV.transform = transform;
}

-(void)handleSwipeAction:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"改变颜色了");
    if (sender.direction ==  UISwipeGestureRecognizerDirectionRight )
    {
        self.view.backgroundColor = [UIColor blackColor];
    }
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        self.view.backgroundColor = [UIColor grayColor];
    }
}

-(void)handleLongPressAction:(UILongPressGestureRecognizer *)sender
{
    for (id type in [self.view subviews])
    {
        if ([type isKindOfClass:[UIActionSheet class]])
        {
            return;
        }
    }
    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"提示"
                                                             delegate:nil
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"下载到本地", nil];
    [actionsheet showInView:self.view];
    [actionsheet release];
    
}

-(void)handlePanAction:(UIPanGestureRecognizer *)sender
{
    UIImageView *imageV = (UIImageView *)[self.view viewWithTag:100];
    
    /*
    CGPoint startPoint;
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        startPoint = [sender locationInView:imageV];
    }
    CGPoint currentPoint;
    if (sender.state == UIGestureRecognizerStateChanged)
    {
        currentPoint = [sender locationInView:imageV];
    }
    float diaX = currentPoint.x - startPoint.x;
    float diaY = currentPoint.y - startPoint.y;
    CGPoint p =imageV.center;
    p.x + = diaX;
    p.y + = diaY;
    
    imageV.center = p;
    */
    // 老师的方法
    /*
    UIImageView * myView = (UIImageView *)[self.view viewWithTag:1000];
    // 获得相对移动量
    CGPoint p = [sender translationInView:myView];
    // 改变中心点
    myView.center = CGPointMake(myView.center.x + p.x, myView.center.y + p.y);
    // 重置坐标零点
    [sender setTranslation:CGPointZero inView:myView];
    */
     
    //CGPoint stratPoint = imageV.frame.origin;
    CGPoint point = [sender translationInView:imageV];
    
    CGPoint p = imageV.center;
    p.x += point.x;
    p.y += point.y ;
    imageV.center = p;
    
    [sender setTranslation:CGPointZero inView:imageV];
    //imageV.frame = CGRectMake(p.x, p.y, imageV.frame.size.width, imageV.frame.size.height);
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值