国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

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
 
 
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 威宁| 姚安县| 苍山县| 重庆市| 永福县| 平凉市| 舞钢市| 鄂托克旗| 密山市| 正镶白旗| 常熟市| 治多县| 黑河市| 锦屏县| 石景山区| 无棣县| 中山市| 冕宁县| 灌阳县| 辉县市| 宝清县| 陕西省| 册亨县| 阿克陶县| 二手房| 石嘴山市| 德令哈市| 花莲市| 铜山县| 贺州市| 通城县| 上虞市| 伊宁县| 浙江省| 那坡县| 汝城县| 临朐县| 德清县| 东城区| 昌宁县| 阳信县|