這篇文章主要為大家展示了“iOS如何實(shí)現(xiàn)手勢(shì)”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“iOS如何實(shí)現(xiàn)手勢(shì)”這篇文章吧。
站在用戶的角度思考問題,與客戶深入溝通,找到景東網(wǎng)站設(shè)計(jì)與景東網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋景東地區(qū)。
具體內(nèi)容如下
效果
細(xì)節(jié)
1.UITouch
#import "ViewController_0.h" @interface ViewController_0 () @property (nonatomic, strong)UILabel *label; @end @implementation ViewController_0 - (void)viewDidLoad { [super viewDidLoad]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; self.label.backgroundColor = [UIColor yellowColor]; self.label.layer.borderWidth = 1; [self.view addSubview:self.label]; UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)]; textlabel.text = @"拖動(dòng)方塊"; [textlabel sizeToFit]; textlabel.font = [UIFont systemFontOfSize:12]; [self.view addSubview:textlabel]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { //1.拿到手勢(shì) UITouch *touch = [touches anyObject]; //2.拿到touch 所在view的坐標(biāo) CGPoint point = [touch locationInView:self.view]; //3.讓label拿到坐標(biāo) self.label.center = point; NSLog(@"1.手指接觸到了屏幕"); } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { //1.拿到手勢(shì) UITouch *touch = [touches anyObject]; //2.拿到touch 所在view的坐標(biāo) CGPoint point = [touch locationInView:self.view]; //3.讓label拿到坐標(biāo) self.label.center = point; NSLog(@"2.手指在屏幕上移動(dòng)"); } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { NSLog(@"3.手指剛離開屏幕"); } - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { NSLog(@"4.手勢(shì)失效了"); } @end
2.UITapGestureRecognizer
#import "ViewController_1.h" @interface ViewController_1 () @property (nonatomic, strong) UILabel *label; @end @implementation ViewController_1 - (void)viewDidLoad { [super viewDidLoad]; UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)]; textlabel.text = @"電腦上操作tap手勢(shì) alt +shift 需要連續(xù)點(diǎn)擊次數(shù)2次"; [textlabel sizeToFit]; textlabel.font = [UIFont systemFontOfSize:12]; [self.view addSubview:textlabel]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 100)]; self.label.backgroundColor = [UIColor orangeColor]; self.label.userInteractionEnabled = YES; self.label.text = @"0"; self.label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.label]; // 1.創(chuàng)建tap手勢(shì) UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)]; tap.numberOfTapsRequired = 2; //需要輕輕點(diǎn)擊的次數(shù) tap.numberOfTouchesRequired = 2;//需要的手指數(shù)量 :2根手指alt +shift 需要匹配點(diǎn)擊次數(shù)2次(其實(shí)直接用默認(rèn)的就好) [self.label addGestureRecognizer:tap]; } - (void)labelTap:(UITapGestureRecognizer *)tap { int num = [self.label.text intValue]; num++; self.label.text = [NSString stringWithFormat:@"%d",num ]; } @end
3.UILongPressGestureRecognizer
#import "ViewController_2.h" @interface ViewController_2 () <UIGestureRecognizerDelegate> @property (nonatomic, strong) UILabel *label; @end @implementation ViewController_2 - (void)viewDidLoad { [super viewDidLoad]; UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)]; textlabel.text = @"long長(zhǎng)按手勢(shì)"; [textlabel sizeToFit]; textlabel.font = [UIFont systemFontOfSize:12]; [self.view addSubview:textlabel]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(40, 150, 200, 150)]; self.label.backgroundColor = [UIColor grayColor]; self.label.userInteractionEnabled = YES; self.label.text = @"0"; self.label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.label]; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)]; longPress.numberOfTapsRequired = 1; longPress.numberOfTouchesRequired = 1; longPress.minimumPressDuration = 1.0; longPress.delegate = self; [self.label addGestureRecognizer:longPress]; } - (void)longPressAction:(UILongPressGestureRecognizer *)longPress { if(longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"手勢(shì)狀態(tài)開始"); } else if(longPress.state == UIGestureRecognizerStateEnded) { NSLog(@"手勢(shì)狀態(tài)結(jié)束"); } else if(longPress.state == UIGestureRecognizerStateChanged) { NSLog(@"手勢(shì)狀態(tài)改變"); NSInteger num = [self.label.text integerValue]; num ++; self.label.text = [NSString stringWithFormat:@"%ld",(long)num]; } } @end
4.UISwipeGestureRecognizer
#import "ViewController_3.h" @interface ViewController_3 () @property (nonatomic, strong) UILabel *label; @end @implementation ViewController_3 - (void)viewDidLoad { [super viewDidLoad]; UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)]; textlabel.text = @"swipe手勢(shì) 向右滑動(dòng)?1,你也可以設(shè)置左劃上劃下劃"; textlabel.numberOfLines = 0; textlabel.font = [UIFont systemFontOfSize:12]; [self.view addSubview:textlabel]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 100)]; self.label.backgroundColor = [UIColor grayColor]; self.label.userInteractionEnabled = YES; self.label.text = @"0"; self.label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.label]; UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;//默認(rèn)就是向右劃 [self.label addGestureRecognizer:swipeGesture]; } -(void)swipeAction:(UISwipeGestureRecognizer *)swipe { if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { NSLog(@"現(xiàn)在響應(yīng)左劃手勢(shì)"); } else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { NSLog(@"現(xiàn)在響應(yīng)右劃手勢(shì)"); NSInteger num = [self.label.text integerValue]; num ++; self.label.text = [NSString stringWithFormat:@"%ld",(long)num]; } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) { NSLog(@"現(xiàn)在響應(yīng)上劃手勢(shì)"); } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) { NSLog(@"現(xiàn)在響應(yīng)下劃手勢(shì)"); } } @end
5.UIPanGestureRecognizer
#import "ViewController_4.h" @interface ViewController_4 () @property (nonatomic, strong) UILabel *label; @end @implementation ViewController_4 - (void)viewDidLoad { [super viewDidLoad]; UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)]; textlabel.text = @"pan手勢(shì),拖動(dòng)方塊"; [textlabel sizeToFit]; textlabel.font = [UIFont systemFontOfSize:12]; [self.view addSubview:textlabel]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; self.label.backgroundColor = [UIColor grayColor]; self.label.userInteractionEnabled = YES; self.label.text = @"0"; self.label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.label]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [self.label addGestureRecognizer:pan]; } - (void)panAction:(UIPanGestureRecognizer *)pan { CGPoint offset = [pan locationInView:self.view]; self.label.center = offset; } @end
6.UIRotationGestureRecognizer
#import "ViewController_5.h" @interface ViewController_5 () @property (nonatomic, strong) UILabel *label; @end @implementation ViewController_5 - (void)viewDidLoad { [super viewDidLoad]; UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)]; textlabel.text = @"模擬器測(cè)試旋轉(zhuǎn)手勢(shì)時(shí),按住 option鍵,再用觸摸板或鼠標(biāo)操作"; textlabel.font = [UIFont systemFontOfSize:12]; textlabel.numberOfLines = 0; [self.view addSubview:textlabel]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 200, 50)]; self.label.backgroundColor = [UIColor grayColor]; self.label.userInteractionEnabled = YES; self.label.text = @"we are friends"; self.label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.label]; //(模擬器測(cè)試捏合和旋轉(zhuǎn)手勢(shì)時(shí),按住 option 鍵,再用觸摸板或鼠標(biāo)操作) UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)]; [self.view addGestureRecognizer:rotation]; } - (void)rotationAction:(UIRotationGestureRecognizer *)rotation { self.label.transform = CGAffineTransformRotate(self.label.transform, rotation.rotation); rotation.rotation = 0; // 這個(gè)很重要!!!!! // static float orginState; // // self.label.transform = CGAffineTransformMakeRotation(rotation.rotation + orginState); // // if (rotation.state == UIGestureRecognizerStateEnded) { // // orginState = orginState + rotation.state; // self.label.transform = CGAffineTransformMakeRotation(rotation.rotation); // } } @end
7.UIPinchGestureRecognizer
#import "ViewController_6.h" @interface ViewController_6 () @property (nonatomic, strong) UILabel *label; @end @implementation ViewController_6 - (void)viewDidLoad { [super viewDidLoad]; UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)]; textlabel.text = @"模擬器測(cè)試捏合手勢(shì)時(shí),按住 option鍵,再用觸摸板或鼠標(biāo)操作"; textlabel.font = [UIFont systemFontOfSize:12]; textlabel.numberOfLines = 0; [self.view addSubview:textlabel]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 80, 80)]; self.label.backgroundColor = [UIColor grayColor]; self.label.userInteractionEnabled = YES; self.label.text = @"0"; self.label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.label]; // (模擬器測(cè)試捏合和旋轉(zhuǎn)手勢(shì)時(shí),按住 option 鍵,再用觸摸板或鼠標(biāo)操作) UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pichGesture:)]; [self.view addGestureRecognizer:pinch]; } - (void)pichGesture:(UIPinchGestureRecognizer *)pinch { static float originScale = 1; //手勢(shì)縮放返回的scale 是相對(duì)于上一次的 self.label.transform = CGAffineTransformMakeScale(pinch.scale * originScale , pinch.scale*originScale); if (pinch.state == UIGestureRecognizerStateEnded) { originScale = originScale * pinch.scale; } } @end
以上是“iOS如何實(shí)現(xiàn)手勢(shì)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站名稱:iOS如何實(shí)現(xiàn)手勢(shì)
文章地址:http://sd-ha.com/article18/jsjidp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、標(biāo)簽優(yōu)化、、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)