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

首頁 > 系統(tǒng) > iOS > 正文

iOS開發(fā)筆記之多點(diǎn)觸控(三)調(diào)用UITouch對象方法——locationInView,多點(diǎn)移動多個對象

2019-11-14 20:43:04
字體:
供稿:網(wǎng)友

屏幕上新建兩個view,實(shí)現(xiàn)兩個手指在屏幕上移動兩個view,通過規(guī)則約束兩個view的活動范圍。

關(guān)鍵在于調(diào)用UITouch對象的方法——lacationInView。此方法返回View的相對于根視圖的觸摸位置。返回值是一個CGPoint類型,是一個包含X坐標(biāo)和Y坐標(biāo)的結(jié)構(gòu)體。我讓兩個view在屏幕上下兩個半?yún)^(qū)水平移動。利用CGPointMake來快速初始化新的CGPoint結(jié)構(gòu)體。

- (void)viewDidLoad{    [super viewDidLoad];    self.view.multipleTouchEnabled = TRUE;    // Do any additional setup after loading the view, typically from a nib.    _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 40, 40)];    _view1.backgroundColor = [UIColor blueColor];    [self.view addSubview:_view1];        _view2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 60, 40, 40)];    _view2.backgroundColor = [UIColor yellowColor];    [self.view addSubview:_view2];}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event //首次在屏幕上檢測到觸摸時調(diào)用{    NSLog(@"touchesBegan");    for (UITouch *touch in touches)    {//        NSLog(@" - %p",touch);        //獲取根視圖內(nèi)觸摸點(diǎn)的point        CGPoint touchPoint = [touch locationInView:self.view];        //約束兩個view的活動范圍        if (touchPoint.y < self.view.frame.size.height/2)        {            _view1.center = CGPointMake(touchPoint.x, _view1.center.y);        }else        {            _view2.center = CGPointMake(touchPoint.x, _view2.center.y);        }    }}//以上對觸摸進(jìn)行了初始化,并未處理沿著屏幕移動的觸摸。所以,只需要在touchesMoved方法里調(diào)用touchesBegan的處理方法來改寫移動球拍的邏輯即可。-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event //如果觸摸移動到了新的位置則會調(diào)用此方法{    NSLog(@"touchesMoved");    for (UITouch *touch in touches)    {//        NSLog(@" - %p",touch);        [self touchesBegan:touches withEvent:event];    }}-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//當(dāng)觸摸離開屏幕調(diào)用此方法{    NSLog(@"touchesEnded");    for (UITouch *touch in touches)    {        NSLog(@" - %p",touch);    }}-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event//如系統(tǒng)決定取消此次觸摸,那可能就不調(diào)用touchesEnded方法了,在這種情況下會調(diào)用touchesCancelled方法{    NSLog(@"touchesCancelled");    for (UITouch *touch in touches)    {        NSLog(@" - %p",touch);    }}

下圖為兩個view可以分別沿著水平方向移動

轉(zhuǎn)載請注明原著來源:http://m.survivalescaperooms.com/marvindev

下一篇介紹iOS開發(fā)筆記之多點(diǎn)觸控(四) 可靠的多點(diǎn)觸控,為每個View分配唯一觸摸對象


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 辰溪县| 屏东县| 襄汾县| 张掖市| 三台县| 启东市| 马尔康县| 万全县| 乌恰县| 逊克县| 通江县| 湖州市| 驻马店市| 抚顺县| 呼伦贝尔市| 长乐市| 淮安市| 孝义市| 乐亭县| 台东市| 彰化县| 星子县| 阿拉善右旗| 仙桃市| 东台市| 雷波县| 北票市| 陵水| 宁陵县| 都兰县| 崇仁县| 瑞丽市| 万年县| 余江县| 福州市| 白城市| 石嘴山市| 丹棱县| 铜鼓县| 友谊县| 安阳县|