UI 06 _ 7種手勢

// UIImageView
    UIImage *image = [UIImage imageNamed:@"u=3179572108,1349777253&fm=21&gp=0.jpg"];
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.frame = CGRectMake(45, 100, 300, 300);
    [self.view addSubview:self.imageView];
    [_imageView release];

1. 點擊

    // 用戶交互默認是關閉的就只有兩個控件,一個是不能夠點擊的UILabel, 一個是UIImageView.我們想要點擊UIImageView需要把用戶交互打開.
    self.imageView.userInteractionEnabled = YES;


 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 設置點擊幾次纔會觸發的方法:
    tap.numberOfTapsRequired = 2;
    //設置幾根手指:
    tap.numberOfTouchesRequired = 2;
    //將手勢添加到對應的圖片上
    [self.imageView addGestureRecognizer:tap];
    [tap release];
#pragma mark 點擊的手勢方法.
- (void)tapAction:(UITapGestureRecognizer *)tap{
    // 點擊改變圖片
    self.imageView.image = [UIImage imageNamed:@"1.jpg"];
}

2.長按

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 設置長按觸發的最小時間
    longPress.minimumPressDuration = 1;
    // 允許用戶手指在長安過程中允許移動的距離,超過範圍就停止.
    longPress.allowableMovement = 200;
    // 把手勢添加到圖片
    [self.imageView addGestureRecognizer:longPress];
    [longPress release];
#pragma  mark 長按的手勢方法.
- (void)longPressAction:(UILongPressGestureRecognizer *)longpress{
    // 長按狀態
   // longpress.state;
    //長按彈出來一個UIAlertView
    if (self.alertView == nil) {
    self.alertView = [[UIAlertView alloc] initWithTitle:@"要刪除麼?" message:@"不要刪除我啊!" delegate:self cancelButtonTitle:@"賣萌可恥,無情地刪除!" otherButtonTitles:@"你這麼可愛,不刪了~", nil];
    [self.alertView show];
    [_alertView release];
    }else{
       [self.alertView show];
    }
}

3.旋轉

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [self.imageView addGestureRecognizer:rotation];
    [rotation release];
#pragma mark 通過旋轉手勢,讓圖片發生旋轉.
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
    // 可以通過手勢獲取手勢添加的視圖是哪一個
    UIImageView *imageview =(UIImageView *) rotation.view;
    NSLog(@"%@",imageview);
    // 進行旋轉操作
    //通過視圖的一個transform屬性,讓視圖進行旋轉.
//    imageview.transform = CGAffineTransformMakeRotation(rotation.rotation);
    imageview.transform = CGAffineTransformRotate(imageview.transform, rotation.rotation);
    rotation.rotation = 0;
}

4.捏合

 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.imageView addGestureRecognizer:pinch];
    [pinch release];
#pragma mark 通過捏合手勢,等比例縮放圖片.
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    // 通過手勢找視圖
    UIImageView *imageview = (UIImageView *)pinch.view;
    // 通過Transform 改變圖片尺寸
    imageview.transform = CGAffineTransformScale(imageview.transform, pinch.scale, pinch.scale);
    pinch.scale = 1;// 給它終止的一個尺寸,不讓照片直接消失.=
}

5. 拖拽

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.imageView addGestureRecognizer:pan];
// 手勢需要指定方向.
    //向右劃
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
#pragma mark 拖拽手勢,讓圖片隨着手勢移動而移動.
- (void)panAction:(UIPanGestureRecognizer *)pan{
   // 通過手勢找視圖
    UIImageView *imageview = (UIImageView *)pan.view;
    //通過手勢獲得經過的點.
   CGPoint p = [pan translationInView:imageview];
    // 設置移動的位置
   imageview.transform = CGAffineTransformTranslate(imageview.transform, p.x, p.y);
    //爲了防止手勢在操作的時候視圖消失.
    [pan setTranslation:CGPointZero inView:imageview];
}

6.輕掃

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [self.imageView addGestureRecognizer:swipe];
    [swipe release];

#pragma  mark 輕掃手勢
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight ) {
        NSLog(@"xiangyou");//需要把拖拽移除.
    }
}

7.屏幕邊際手勢,iOS7.0之後出現的手勢.

UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgeAction:)];
    [self.imageView addGestureRecognizer:screenEdge
     ];
    [screenEdge release];

對應的方法還沒有添加呢!

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