国产探花免费观看_亚洲丰满少妇自慰呻吟_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://www.survivalescaperooms.com/marvindev

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沂源县| 定陶县| 琼结县| 丰镇市| 县级市| 凤冈县| 东辽县| 杭锦后旗| 茂名市| 博乐市| 布尔津县| 昌黎县| 岳普湖县| 英吉沙县| 临夏县| 登封市| 开鲁县| 绿春县| 长丰县| 巴青县| 桦川县| 织金县| 宜兰市| 唐海县| 班玛县| 金溪县| 汶上县| 宜黄县| 泸西县| 天门市| 延寿县| 蒲城县| 高青县| 荆州市| 元朗区| 中阳县| 鄂尔多斯市| 洛隆县| 通山县| 固安县| 鄯善县|