iOS學習必須瞭解的七大手勢

來源:互聯網
上載者:User

標籤:支援   建立對象   ssd   事件   旋轉   應該   一個   atom   狀態   

文章只要你有一點點基礎應該就可以看的懂,文章只為學習交流

#import "ViewController.h"@interface ViewController ()@property (nonatomic,retain)UIImageView *imageView;@property (nonatomic,assign)NSInteger index;//下標@property (nonatomic,retain)NSMutableArray *images;//圖片名 字數組@end@implementation ViewController#載入視圖-(void)viewDidLoad{[super viewDidLoad];//布局imageView[self layoutImageView];//1.建立輕拍手勢[self tapGestureRecognizer];//2.建立清掃手勢[self swipeGestureRecognizer];//3.建立長安手勢[self longPressGestureRecognizer];//4.建立平移手勢[self panGestureTecognizer];//5.建立捏合手勢[self pinchGestureRecognizer];//6.建立 旋轉手勢[self rotationGestureRecognizer]//7.建立側邊手勢[self screenEdgePanGestureRecognizer];

}

##布局ImageView-(void)layoutImageView{//建立對象UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];//配置屬性imageView.backgroundColor =[UIColor purpleColor];//設定圖片imageView.image =[UIImage imageNamed:@"car1.jpg"];//添加父視圖[self.view addSubview:imageView];//將建立的圖片視圖對象 給屬性賦值self.imageView =imageView;//開啟使用者互動self.imageView.userInteractionEnabled =YES;self.images =[NSMutableArray array];for (int i = 1; i<9; i++) { NSString * imageName =[NSString stringWithFormat:@"car%d.jpg",i]; [_images addObject:imageName];}// _index =1;}#pragma 輕怕手勢//建立輕拍手勢-(void)tapGestureRecognizer{//建立手勢對象UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];//配置屬性//輕拍次數tap.numberOfTapsRequired =1;//輕拍手指個數tap.numberOfTouchesRequired =1;//講手勢添加到指定的視圖上[_imageView addGestureRecognizer:tap];}//輕拍事件-(void)tapAction:(UITapGestureRecognizer *)tap{//圖片切換NSLog(@"拍一下");_index ++;if (_index == 8) { _index = 1;}self.imageView.image =[UIImage imageNamed:_images[_index]];}#pragma 清掃手勢//清掃手勢-(void)swipeGestureRecognizer{UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];//配置屬性//一個清掃手勢 只能有兩個方向(上和下) 或者 (左或右)//如果想支援上下左右清掃 那麼一個手勢不能實現 需要建立兩個手勢swipe.direction =UISwipeGestureRecognizerDirectionLeft;//添加到指定視圖[_imageView addGestureRecognizer:swipe];UISwipeGestureRecognizer *swipe2 =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];swipe2.direction =UISwipeGestureRecognizerDirectionRight;[_imageView addGestureRecognizer:swipe2];}//清掃事件-(void)swipeAction:(UISwipeGestureRecognizer *)swipe{NSLog(@"掃一下");if (swipe.direction ==UISwipeGestureRecognizerDirectionRight) { NSLog(@"右清掃"); _index --; if (_index < 1) { _index =7; } _imageView.image =[UIImage imageNamed:_images[_index]];}else if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){ NSLog(@"左掃一下"); _index ++; if (_index == 8) { _index=1; } _imageView.image =[UIImage imageNamed:_images[_index]];}}#pragma 長按手勢//建立長按手勢-(void)longPressGestureRecognizer{UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];//最短長按時間longPress.minimumPressDuration =2;//允許移動最大距離longPress.allowableMovement =1;//添加到指定視圖[_imageView addGestureRecognizer:longPress];}//長按事件-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{//NSLog(@"長按");//對於長安有開始和 結束狀態if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"長按開始"); //將圖片儲存到相簿 UIImage *image =[UIImage imageNamed:_images[_index]]; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);}else if (longPress.state == UIGestureRecognizerStateEnded){ NSLog(@"長按結束");}}#pragma 平移手勢//建立平移手勢-(void)panGestureTecognizer{UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];//添加到指定視圖[_imageView addGestureRecognizer:pan];}//建立平移事件-(void)panAction:(UIPanGestureRecognizer *)pan{//擷取手勢的位置CGPoint position =[pan translationInView:_imageView];//通過stransform 進行平移交換_imageView.transform = CGAffineTransformTranslate(_imageView.transform, position.x, position.y);//將增量置為零[pan setTranslation:CGPointZero inView:_imageView];}#pragma 捏合手勢-(void)pinchGestureRecognizer{UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];//添加到指定視圖[_imageView addGestureRecognizer:pinch];}//添加捏合事件-(void)pinchAction:(UIPinchGestureRecognizer *)pinch{//通過 transform(改變) 進行視圖的視圖的捏合_imageView.transform =CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);//設定比例 為 1pinch.scale = 1;}#pragma 旋轉手勢//建立旋轉手勢-(void)rotationGestureRecognizer{UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];//添加到指定的視圖[_imageView addGestureRecognizer:rotation];}//旋轉事件-(void)rotationAction:(UIRotationGestureRecognizer *)rote{//通過transform 進行旋轉變換_imageView.transform = CGAffineTransformRotate(_imageView.transform, rote.rotation);//將旋轉角度 置為 0rote.rotation = 0;}#pragma 側邊手勢//建立側邊手勢-(void)screenEdgePanGestureRecognizer{UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPanAction:)];[_imageView addGestureRecognizer:screenPan];}//建立邊緣事件-(void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan{NSLog(@"邊緣");}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end

iOS學習必須瞭解的七大手勢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.