麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

iOS階段學習第35天筆記(Touch手勢介紹)

2019-11-14 18:25:37
字體:
來源:轉載
供稿:網友

一、Touch手勢

1、利用手勢實現UIButton移動效果  實例代碼     


1) 創建一個繼承自UIButton的類 MyButton.h  代碼實現 

1 #import <UIKit/UIKit.h>2 @interface MyButton : UIButton3 @end

2)MyButton.m  的代碼實現

 1 #import "MyButton.h" 2 @implementation MyButton 3 { 4     CGPoint _lastPoint; 5 } 6  7 //手勢開始 8 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 9 {10     UITouch *touch = [touches anyObject];11     CGPoint point = [touch locationInView:self];12     NSLog(@"began:%@",NSStringFromCGPoint(point));13     _lastPoint = point;14 }15 16 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event17 {18     UITouch *touch = [touches anyObject];19     CGPoint point = [touch locationInView:self];  20     CGFloat offsetx = point.x - _lastPoint.x;21     CGFloat offsety = point.y - _lastPoint.y; 22     self.center = CGPointMake(self.center.x + offsetx, self.center.y + offsety);   23     NSLog(@"moved:%@",NSStringFromCGPoint(point));24 }25 26 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event27 {28     UITouch *touch = [touches anyObject];29     CGPoint point = [touch locationInView:self];30     NSLog(@"end:%@",NSStringFromCGPoint(point));31 }32 @end 

3)父視圖中的代碼實現

 1 #import "ViewController.h" 2 #import "MyButton.h" 3 @interface ViewController () 4 { 5     MyButton *_v; 6     CGPoint _lastPoint; 7 } 8 @end 9 10 @implementation ViewController11 12 - (void)viewDidLoad {13     [super viewDidLoad];14     _v = [[MyButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];15  _v.backgroundColor = [UIColor redColor];16     [self.view addSubview:_v];17 }18 19 //手勢開始20 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event21 {22     UITouch *touch = [touches anyObject];23     CGPoint point = [touch locationInView:self.view];24     NSLog(@"began:%@",NSStringFromCGPoint(point));25     _lastPoint = point;26 }27 28 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event29 {30     UITouch *touch = [touches anyObject];31     CGPoint  point = [touch locationInView:self.view];32     CGFloat  offsetx = point.x - _lastPoint.x;33     CGFloat  offsety = point.y - _lastPoint.y; 34     _v.center = CGPointMake(_v.center.x + offsetx, _v.center.y + offsety);     35     _lastPoint = point;     36     NSLog(@"moved:%@",NSStringFromCGPoint(point));37 }38 39 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event40 {41     UITouch *touch = [touches anyObject];42     CGPoint  point = [touch locationInView:self.view];43     NSLog(@"end:%@",NSStringFromCGPoint(point));44 }45 46 -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event47 {48     49 }50 @end

2、利用Touch手勢實現控件的縮放與旋轉效果 實例代碼

 1 #import "ViewController.h" 2 //遵守旋轉與縮放的代理協議 3 @interface ViewController ()<UIGestureRecognizerDelegate> 4 @end 5  6 @implementation ViewController 7  8 - (void)viewDidLoad { 9     [super viewDidLoad];10     11     UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];12     imgv.center = self.view.center;13     [self.view addSubview:imgv];14     imgv.image = [UIImage imageNamed:@"3"];15     imgv.userInteractionEnabled = YES;16     17     //點擊手勢18     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)];19     20     //設置該手勢需要的手指數21     tap.numberOfTouchesRequired = 2;22     23     //設置該手勢的點擊次數24     tap.numberOfTapsRequired = 4;25     [imgv addGestureRecognizer:tap];26     27     //平移手勢,拖拽手勢28     UipanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];29     [imgv addGestureRecognizer:pan];30     31     //縮放手勢32     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGes:)];33     [imgv addGestureRecognizer:pinch];34     pinch.delegate = self;35     36     //旋轉37     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];38     [imgv addGestureRecognizer:rotation];39     rotation.delegate = self;40 }41 42 //返回值表示能否同時識別其他(相對于已經設置了代理的手勢)手勢43 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer44 {45 return YES;46 }47 -(void)rotationGes:(UIRotationGestureRecognizer *)rotation48 {49 rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);50 rotation.rotation = 0.0;51 }52 -(void)pinchGes:(UIPinchGestureRecognizer *)pinch53 {54 //transform:仿射變換55 //pinch.scale,是縮放手勢的捏合倍率56 pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);57 58 //倍率還原59 pinch.scale = 1.0;60 }61 -(void)panGes:(UIPanGestureRecognizer *)pan62 {63 //返回當前的手勢的偏移量64 CGPoint offset = [pan translationInView:pan.view];65 //pan.view就是pan手勢所加到的視圖66 pan.view.center = CGPointMake(pan.view.center.x + offset.x, pan.view.center.y + offset.y);67 //移動以后,把偏移量歸068 [pan setTranslation:CGPointZero inView:pan.view];69 }70 71 -(void)tapGes:(UIGestureRecognizer *)tap72 {73 NSLog(@"==========");74 }75 @end
 
 
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一级黄色淫片 | 美女网站黄在线观看 | 一级在线视频 | 国产精品刺激对白麻豆99 | 黄色大片在线观看 | 精品久久久久久综合日本 | 夜夜b| 欧美一级全黄 | 深夜福利视频免费观看 | 51色视频 | 日本成年免费网站 | av成人在线电影 | 国产精品久久久久久久久久 | 黄色毛片免费视频 | 久久伊人国产精品 | 成人毛片100免费观看 | 亚洲男人一区 | 最近国产中文字幕 | 成人免费电影在线观看 | 色妞妞视频 | 久久久久久久免费视频 | 久久久一区二区精品 | 久久亚洲美女视频 | 渔夫荒淫艳史 | 在线中文资源免费 | 黄色免费不卡视频 | 国产精品99久久免费观看 | 91看片片| 国产精品成人av片免费看最爱 | 久久精品国产久精国产 | 在线看一区二区三区 | 2021国产精品 | 午夜精品福利视频 | 亚洲精品一区二区三区大胸 | 日韩av电影免费在线观看 | 91精品国| 韩国一大片a毛片 | 中国杭州少妇xxxx做受 | 最新午夜综合福利视频 | 国产宾馆3p国语对白 | 欧美成年人视频在线观看 |