创建一个可移动的视图,让视图随着鼠标的移动而移动
2014-04-19 17:59
357 查看
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;//触摸取消(主要用于突发事件,此次触摸失效,例如来电话了,电话界面弹出,用户此次触摸被取消)
可以根据这几个方法对一些触摸事件进行处理。
相关文章推荐
- 创建一个可移动的视图,让视图随着鼠标的移动而移动
- MFC小程序003------MFC使用WebBrowser组件,在对话框中创建滚动视图,动态创建一个静态文本控件并设置鼠标单击的消息响应
- 捏合视图变化(视图的缩放), 单击随着鼠标移动, 双击放大视图,再双击还原视图
- 使用键盘来改变形状中的线条样式,或移动鼠标来创建一个新的形状
- UITouch(事件)及其如何让视图随着鼠标移动
- 刚用Mootools写了一个随着鼠标移动而背景图也跟着移动的东西
- 分享一个Unity3D点击模型跟随鼠标移动的小脚本(包括屏幕视图到三维空间的坐标转换)
- MFC小程序003------MFC使用WebBrowser组件,在对话框中创建滚动视图,动态创建一个静态文本控件并设置鼠标单击的消息响应
- 刚用Mootools写了一个随着鼠标移动而背景图也跟着移动的东西
- 使用c#创建一个可以监视全局鼠标位置的程序
- androi View视图框架源码分析之一:android是如何创建一个view的
- ASP.NET MVC项目里创建一个aspx视图
- 怎么在同一图片框内随着鼠标移动来变换显示图片?
- 用CSS3和jQuery来创建一个鼠标悬停显示详细面板
- 写sql语句创建视图的一个例子
- NGUI 点击屏幕时在该点创建图片并跟随鼠标移动
- 一个响应鼠标移动的简单实例!
- 鼠标移动到一个位置弹出相应的一张图片
- 带参数的存储过程动态创建一个视图及调用方法[表不固定,作为参数]
- MFC多文档程序启动时一个文档创建两个视图