创建一个可移动的视图,让视图随着鼠标的移动而移动
2016-02-01 15:03
435 查看
1.首先自己定义视图,继承自UIView
MoveView.h
@interface MoveView :UIView
@property(nonatomic,assign)CGPoint beginPoint;
//记录点击的最初位置
@end
MoveView.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches
anyObject];
//记录下触摸的最初位置。相对于自身
_beginPoint=[touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches
anyObject];
//记录移动到的位置,此时此时相对于父视图
CGPoint endPoint=[touch
locationInView:self.superview];
[self setFrame:CGRectMake(endPoint.x-_beginPoint.x
, endPoint.y-_beginPoint.y,self.frame.size.width,self.frame.size.height)];
}
2.查UIView的Api可知,UIView继承自UIResponder,在UIResponder中有以下四个方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;//触摸開始
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;//触摸移动
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;//触摸结束
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;//触摸取消(主要用于突发事件,此次触摸失效,比如来电话了,电话界面弹出。用户此次触摸被取消)
能够依据这几个方法对一些触摸事件进行处理。
MoveView.h
@interface MoveView :UIView
@property(nonatomic,assign)CGPoint beginPoint;
//记录点击的最初位置
@end
MoveView.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches
anyObject];
//记录下触摸的最初位置。相对于自身
_beginPoint=[touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches
anyObject];
//记录移动到的位置,此时此时相对于父视图
CGPoint endPoint=[touch
locationInView:self.superview];
[self setFrame:CGRectMake(endPoint.x-_beginPoint.x
, endPoint.y-_beginPoint.y,self.frame.size.width,self.frame.size.height)];
}
2.查UIView的Api可知,UIView继承自UIResponder,在UIResponder中有以下四个方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;//触摸開始
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;//触摸移动
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;//触摸结束
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;//触摸取消(主要用于突发事件,此次触摸失效,比如来电话了,电话界面弹出。用户此次触摸被取消)
能够依据这几个方法对一些触摸事件进行处理。
相关文章推荐
- Codeforces Round #341 (Div. 2) D. Rat Kwesh and Cheese(数学 | 复数)
- jQuery对象与DOM对象的互相转化
- 改变libvirt VM镜像的默认位置的方法
- 解决 Google Chrome 读取 favicon.ico 错误
- Matlab的logo是怎么来的? L形区域上的波动方程的解
- 将JSON格式化输出为模型的属性
- linux笔记
- nginx与Apache的涉及的计算机原理说明
- java WeakReference
- javascript 变量转义
- java中ResultSet遍历数据
- ../gems/json-1.8.0/lib/json/common.rb:67: [BUG] Segmentation fault
- Spring定时调度器应用
- 波浪交互设计与技术实现
- Light OJ 1013 Love Calculator (dp)
- 工厂模式的作用
- Thrift的TJsonProtocol协议分析
- php数组函数常见的那些
- js获取fck内容
- Java:单例模式的七种写法