iOS 手勢應用

.h文件

#import <UIKit/UIKit.h>

 @interface RootViewController : UIViewController

{

    UIImageView *imageView;

}

@end





.m文件


#import "RootViewController.h"

@interface RootViewController ()



@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

//給View添加背景顏色
    self.view.backgroundColor =[UIColorwhiteColor];

 
//創建UIImageView
    imageView =[[UIImageViewalloc]initWithFrame:CGRectMake(80, 100, 100, 100)];

    imageView.backgroundColor =[UIColorredColor];

    [self.view addSubview:imageView];
//開啓交互
    imageView.userInteractionEnabled = YES;

    //    實現點擊方法

    UITapGestureRecognizer *tap =[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapHand:)];

    //    點擊次數

    tap.numberOfTapsRequired = 1;

    [imageView addGestureRecognizer:tap];

    [tap release];

    

    //    實現長按方法

    UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];

    //    長按需要多長時間纔有效

    longPress.minimumPressDuration = 2;

    //    允許偏移距離

    longPress.allowableMovement = 100;

    [imageViewaddGestureRecognizer:longPress];

    [longPress release];

    //    輕掃手勢

    UISwipeGestureRecognizer *swip =[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipPress:)];

    //    輕掃的方向,(向左)

    swip.direction=UISwipeGestureRecognizerDirectionLeft;

    [imageView addGestureRecognizer:swip];

    [swip release];

    //    旋轉手勢

    UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationP:)];

    [imageViewaddGestureRecognizer:rotation];

    [rotation release];

    //    拖拽方法

    UIPanGestureRecognizer *pan =[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePan:)];

    [imageView addGestureRecognizer:pan];

    [pan release];

}

//點擊方法

-(void)tapHand:(UITapGestureRecognizer*)tap

{

    

}

//長按方法

-(void)longPress:(UILongPressGestureRecognizer *)longP

{

    if (longP.state == UIGestureRecognizerStateBegan) {

        NSLog(@"開始");

    }else if (longP.state == UIGestureRecognizerStateChanged)

    {

        NSLog(@"移動");

    }else if (longP.state == UIGestureRecognizerStateEnded)

    {

        NSLog(@"結束");

    }

}

//輕掃方法

-(void)swipPress:(UISwipeGestureRecognizer*)swip

{

    if (swip.direction==UISwipeGestureRecognizerDirectionLeft) {

        NSLog(@"圖片移動");

    }

}

//旋轉手勢方法

-(void)rotationP:(UIRotationGestureRecognizer *)rotationP

{

    rotationP.view.transform=CGAffineTransformRotate(rotationP.view.transform, rotationP.rotation);

    rotationP.rotation = 0;

}

//拖拽手勢方法

-(void)handlePan:(UIPanGestureRecognizer*)recognizer

{

    CGPoint translation = [recognizer translationInView:self.view];

    recognizer.view.center=CGPointMake(recognizer.view.center.x+translation.x,recognizer.view.center.y+translation.y);

    [recognizer setTranslation:CGPointZeroinView:self.view];

    if(recognizer.state==UIGestureRecognizerStateEnded){

        CGPoint velocity = [recognizer velocityInView:self.view];

        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));

        CGFloat slideMult = magnitude / 200;

        NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

        float slideFactor = 0.1*slideMult;// Increase for more of a slide

        CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),recognizer.view.center.y +(velocity.y * slideFactor));

        finalPoint.x = MIN(MAX(finalPoint.x, 0),self.view.bounds.size.width);

        finalPoint.y = MIN(MAX(finalPoint.y,0),self.view.bounds.size.height);

        [UIViewanimateWithDuration:slideFactor*2 delay:0options:UIViewAnimationOptionCurveEaseOutanimations:^{recognizer.view.center=finalPoint;}completion:nil];

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章