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

來源:互聯網
上載者:User

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

一、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
 

相關文章

聯繫我們

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