iOS UI05_手勢辨識器

來源:互聯網
上載者:User

標籤:手勢   transform   

手勢:點擊,長按,旋轉,捏合,拖拽,清掃,清掃的方向

////  MainViewController.m//  UI05_手勢辨識器////  Created by dllo on 15/8/4.//  Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import "MainViewController.h"@interface MainViewController ()@property(nonatomic,retain)UIImageView *imageView;@property(nonatomic,retain)UIAlertView *alertView;@end@implementation MainViewController-(void)dealloc{    [_imageView release];    [_alertView release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    //UIImageView    UIImage *image=[UIImage imageNamed:@"footRight_03.jpg.jpg"];    self.imageView=[[UIImageView alloc] initWithImage:image];    self.imageView.frame=CGRectMake(50, 100, 300, 400);    [self.view addSubview:self.imageView];    [_imageView release];    //把圖片的使用者互動開啟,預設是關閉的,此外還有一個控制項是label    self.imageView.userInteractionEnabled=YES;    //手勢的使用    //1.點擊    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];    //設定點擊幾次才會觸發方法    tap.numberOfTapsRequired=2;    //設定幾根手指進行點擊    tap.numberOfTouchesRequired=2;    //將手勢添加到對應的圖片上    [self.imageView addGestureRecognizer:tap];    [tap release];     //2.長按    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];    //設定長按觸發的最短時間    longPress.minimumPressDuration =2;    //使用者手指在長按過程中允許移動的距離    longPress.allowableMovement=200;    //把手勢添加到圖片上    [self.imageView addGestureRecognizer:longPress];    [longPress release];    //3.旋轉    //建立一個旋轉的手勢    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];    //把手勢放到對應的圖片上    [self.imageView addGestureRecognizer:rotation];    //釋放    [rotation release];    //4.捏合    //建立    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pincheAction:)];    //添加到圖片上    [self.imageView addGestureRecognizer:pinch];    //釋放    [pinch release];    //5.拖拽    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];    [self.imageView addGestureRecognizer:pan];    [pan release];    //6.清掃    UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];    [self.imageView addGestureRecognizer:swipe];    [swipe release];    //清掃的方向    swipe.direction=UISwipeGestureRecognizerDirectionUp;    //螢幕邊界手勢 iOS7.0之後出現的手勢//    UIScreenEdgePanGestureRecognizer}#pragma mark 點擊方法-(void)tapAction:(UITapGestureRecognizer *)tap{    NSLog(@"djkdajdo");    self.imageView.image=[UIImage imageNamed:@"angry_05.jpg"];}#pragma mark 長按時回應程式法-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{    //長按的狀態//    longPress.state    //長按之後彈出一個UIAlerView    if (!self.alertView) {            self.alertView=[[UIAlertView alloc] initWithTitle:@"404" message:@"報錯" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:nil, nil];        [self.alertView show];        [_alertView release];    }}#pragma mark 通過圖片的旋轉手勢,讓圖片發生旋轉-(void)rotationAction:(UIRotationGestureRecognizer *)rotarion{    //可以通過手勢擷取手勢添加的視圖是哪一個    UIImageView *imageView=(UIImageView *)rotarion.view;    //進行旋轉的操作    //通過視圖的transform屬性,讓視圖進行旋轉    imageView.transform=CGAffineTransformRotate(imageView.transform,rotarion.rotation);    rotarion.rotation=0;}#pragma mark 通過捏合手勢,縮放圖片-(void)pincheAction:(UIPinchGestureRecognizer *)pinche{    //通過手勢找視圖    UIImageView *imageView=(UIImageView *)pinche.view;    //通過transform改變圖片的尺寸    imageView.transform=CGAffineTransformScale(imageView.transform, pinche.scale, pinche.scale);    pinche.scale=1;}#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];}#pragma mark 清掃的對應方法-(void)swipeAction:(UISwipeGestureRecognizer *)swipe{    if (swipe.direction == UISwipeGestureRecognizerDirectionUp ) {        NSLog(@"上");    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

iOS UI05_手勢辨識器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.