亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[IOS]觸摸事件和手勢

發布時間:2020-07-09 05:40:07 來源:網絡 閱讀:464 作者:蓬萊仙羽 欄目:移動開發

如何使用IOS中的觸摸事件和手勢,這也是增加我們IOS應用的一個重要的一個功能?下面我來用一個簡單的Demo來入門一下吧!

實現的功能具備右滑動和雙擊操作:

[IOS]觸摸事件和手勢

雙擊切換圖片:

[IOS]觸摸事件和手勢

友情提醒:要實現兩點滑動,按住alt鍵和shift鍵試試

操作步驟:

1.創建一個SingleView的項目,在頁面上添加一個子view和幾個label;

2.ViewController.h:

#import <UIKit/UIKit.h>  @interface DXWViewController : UIViewController @property (retain, nonatomic) IBOutlet UILabel *state; @property (retain, nonatomic) IBOutlet UILabel *tapCount; @property (retain, nonatomic) IBOutlet UILabel *xPoint; @property (retain, nonatomic) IBOutlet UILabel *yPoint; @property (retain, nonatomic) IBOutlet UILabel *touchNum; @property (retain, nonatomic) IBOutlet UIView *otherView; @property(retain,nonatomic)UIImageView *imgView; @property(retain,nonatomic)UIImage *img; @end

3.ViewController.m:

#import "DXWViewController.h"  @interface DXWViewController ()  @end  @implementation DXWViewController  //加載圖片 -(void)loadimag:(NSString *)imgName {     self.img = [UIImage imageNamed:imgName];     self.imgView = [[UIImageView alloc] initWithImage:self.img];     CGRect rec = CGRectMake(5, 5, 150, 117);     self.imgView.frame = rec;     [self.otherView addSubview:self.imgView];          [self.imgView release]; }  - (void)viewDidLoad {     [super viewDidLoad];     self.img = [UIImage imageNamed:@"flower1.jpg"]; 	//花開     [self loadimag:@"flower1.jpg"];      }  -(void)fun:(NSSet *)touches {     UITouch *touch = [touches anyObject];     int tapCount = [touch tapCount];     CGPoint point = [touch locationInView:self.view];          self.tapCount.text = [NSString stringWithFormat:@"tapCount %d",tapCount];     int touchNum = [touches count];     self.xPoint.text = [NSString stringWithFormat:@"x %.1f",point.x];     self.yPoint.text = [NSString stringWithFormat:@"y %.1f",point.y];     //接受多點,要設置允許多點     self.touchNum.text = [NSString stringWithFormat:@"touchNum %d",touchNum];  } //返回點擊次數 -(int)getNumber:(NSSet *)touches {     UITouch *touch = [touches anyObject];     int tapCount = [touch tapCount];     CGPoint point = [touch locationInView:self.view];          self.tapCount.text = [NSString stringWithFormat:@"tapCount %d",tapCount];     int touchNum = [touches count];     self.xPoint.text = [NSString stringWithFormat:@"x %.1f",point.x];     self.yPoint.text = [NSString stringWithFormat:@"y %.1f",point.y];     //接受多點,要設置允許多點     self.touchNum.text = [NSString stringWithFormat:@"touchNum %d",touchNum];     return tapCount;      } BOOL flag = true;  float x=0;  //支持多點 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {          //如果有子view的話如何獲取子view的觸摸事件     //touches = [event touchesForView:self.otherView];     self.state.text = @"touchesBegin";     [self fun:touches];     [self fun:[event touchesForView:self.otherView]];               if ([self getNumber:[event touchesForView:self.otherView]] == 2) {         if (flag) {                          [self loadimag:@"flower.jpg"];             flag = false;         }         else         {             [self loadimag:@"flower1.jpg"];             flag = true;         }     }               UITouch *touch = [touches anyObject];     CGPoint point = [touch locationInView:self.otherView];     x = point.x;          } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {      } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {      } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {     //自己自定義手勢     UITouch *touch = [touches anyObject];     CGPoint point = [touch locationInView:self.otherView];     float moveX = 0;     moveX = point.x;     if ((moveX-x)>100) {         NSLog(@"右滑");     } }  - (void)dealloc {     [_state release];     [_tapCount release];     [_xPoint release];     [_yPoint release];     [_touchNum release];     [_otherView release];     [super dealloc]; } @end 

4.要注意,要把頁面設置一下,設置能多點識別的屬性。

5.也可以通過拖放一個手勢控件來實現手勢,在頁面中有一個Swipe Gesture Recognizer的手勢控件

- (void)viewDidLoad {     [super viewDidLoad];     //代碼創建手勢     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gesture:)];     swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;          //添加上手勢,控件添加     [self.view addGestureRecognizer:swipe]; }  -(void)gesture:(id)sender {     NSLog(@"left or right"); }  - (IBAction)swipe:(id)sender {     NSLog(@"right"); }

6.實現單擊/雙擊觸發事件        

-(void)viewLoad { //點擊事件     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun1)];     //單點觸摸     tap.numberOfTouchesRequired = 1;     //點擊幾次,如果是1就是單擊     tap.numberOfTapsRequired = 1;     [self.view addGestureRecognizer:tap]; }  //一次點擊事件 -(void)fun1 {     NSLog(@"click1"); } 

==================== 迂者 丁小未 CSDN博客專欄=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互學習,共同進步 ===================

 

轉載請注明出處:http://blog.csdn.net/dingxiaowei2013/article/details/17587497

歡迎關注我的微博:http://weibo.com/u/2590571922


                                           

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

抚顺县| 阿拉善左旗| 城固县| 隆子县| 南宁市| 贵德县| 舞钢市| 金平| 巨鹿县| 丹寨县| 舒兰市| 荥阳市| 哈密市| 从江县| 宁陕县| 钦州市| 泗水县| 门源| 巴林左旗| 从江县| 惠水县| 福州市| 兴城市| 甘孜| 武宣县| 奈曼旗| 永顺县| 富川| 饶阳县| 阳信县| 湖北省| 偏关县| 祁东县| 乳山市| 桂东县| 墨玉县| 淮安市| 内黄县| 赣州市| 舞钢市| 舞阳县|