IOS学习之——UITouch 控件的学习

来源:互联网 发布:大数据安全保密制度 编辑:程序博客网 时间:2024/06/05 16:48

//***************************** UITouch 属性 ******************************** //

    触摸产生时所处的窗口    @property(nonatomic,readonly,retain) UIWindow    *window;    触摸产生时所处的视图    @property(nonatomic,readonly,retain) UIView      *view;    短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击    @property(nonatomic,readonly) NSUInteger          tapCount;    记录了触摸事件产生或变化时的时间,单位是秒    @property(nonatomic,readonly) NSTimeInterval      timestamp;    当前触摸事件所处的状态    @property(nonatomic,readonly) UITouchPhase        phase;

//***************************** UITouch 方法 ******************************** //

■   - (CGPoint)locationInView:(UIView *)view;  //当前触摸的位置➢   返回值表示触摸在view上的位置➢   这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))➢   调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置➢   •   - (CGPoint)previousLocationInView:(UIView *)view; // 移动的时候是从之前的那个点 移动到下一个点➢   该方法记录了前一个触摸点的位置            

//***************************** UITouch 例子 ******************************** //

-(void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event{

return;NSLog(@"%s",__func__);// 获取当前触摸点的UITouch对象UITouch *touch = [touches anyObject];// 触摸点 所在的viewNSLog(@"%@",touch.view);// tapCount 点击的次数NSLog(@"%ld",touch.tapCount);// 获取当前 触摸的位置

warning 传一个nil 获取的触摸点的位置是在windows上去

CGPoint touchPosition = [touch locationInView:touch.view];NSLog(@"%@",NSStringFromCGPoint(touchPosition));

}

-(void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event{
//NSLog(@”%s”,func);
// 获取当前触摸点的UITouch对象
UITouch *touch = [touches anyObject];

//获取 当前触摸的位置CGPoint current = [touch locationInView:touch.view];//获取 触摸前的位置CGPoint previous = [touch previousLocationInView:touch.view];NSLog(@"触摸前的点: %@ 当前触摸点:%@",NSStringFromCGPoint(previous),    NSStringFromCGPoint(current)); //**********        可以拖动的按钮   悬浮的按钮   能应用到游戏中     // 改变位置CGPoint center = self.center;center.x += current.x - previous.x;center.y += current.y - previous.y;self.center = center;

}

0 0
原创粉丝点击