拖动视图

http://blog.csdn.net/yiyaaixuexi/article/details/7388142

预备知识

iOS处理屏幕上的触摸动作,主要涉及到以下几个方法:

touchesBegan:withEvent:          //触摸屏幕的最开始被调用

touchesMoved:withEvent:         //移动过程中被调用

touchesEnded:withEvent:         //动作结束时被调用

touchesCancelled:WithEvent:

从方法的命名可以清晰的看出该方法何时被调用,最后一个比较特殊。touchesCancelled:WithEvent:Cocoa Touch必须响应持续触摸事件的系统中断时调用。

我们只要重写这些方法,来作我们想要作的事情就可以了。


如何实现拖动视图?

1.设置userInteractionEnabled属性为YES,允许用户交互。
2.在触摸动作开始时记录起始点。
3.在移动过程中,计算当前位置坐标与起始点的差值,即偏移量,并且移动视图中心点至偏移量大小的地方。
4.分别限制x坐标、与y坐标,保证用户不可将视图托出屏幕

备注:分别限制x坐标与y坐标的原因是,即使向右拖动不了了,仍需保证可以向下拖动。


实现代码

以子类化UIImageView为例

[plain]  view plain copy print ?
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface GragView : UIImageView  
  4. {  
  5.     CGPoint startPoint;  
  6. }  
  7. @end  

[plain]  view plain copy print ?
  1. #import "GragView.h"  
  2.   
  3. @implementation GragView  
  4.   
  5. - (id)initWithFrame:(CGRect)frame  
  6. {  
  7.     self = [super initWithFrame:frame];  
  8.     if (self) {  
  9.         // Initialization code  
  10.         //允许用户交互  
  11.         self.userInteractionEnabled = YES;  
  12.     }  
  13.     return self;  
  14. }  
  15.   
  16. - (id)initWithImage:(UIImage *)image  
  17. {  
  18.     self = [super initWithImage:image];  
  19.     if (self) {  
  20.         //允许用户交互  
  21.         self.userInteractionEnabled = YES;  
  22.     }  
  23.     return self;  
  24. }  
  25.   
  26. - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  27. {  
  28.     //保存触摸起始点位置  
  29.     CGPoint point = [[touches anyObject] locationInView:self];  
  30.     startPoint = point;  
  31.       
  32.     //该view置于最前  
  33.     [[self superview] bringSubviewToFront:self];  
  34. }  
  35.   
  36. -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  37. {  
  38.     //计算位移=当前位置-起始位置  
  39.     CGPoint point = [[touches anyObject] locationInView:self];  
  40.     float dx = point.x - startPoint.x;  
  41.     float dy = point.y - startPoint.y;  
  42.       
  43.     //计算移动后的view中心点  
  44.     CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);  
  45.       
  46.       
  47.     /* 限制用户不可将视图托出屏幕 */  
  48.     float halfx = CGRectGetMidX(self.bounds);  
  49.     //x坐标左边界  
  50.     newcenter.x = MAX(halfx, newcenter.x);  
  51.     //x坐标右边界  
  52.     newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);  
  53.       
  54.     //y坐标同理  
  55.     float halfy = CGRectGetMidY(self.bounds);  
  56.     newcenter.y = MAX(halfy, newcenter.y);  
  57.     newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);  
  58.       
  59.     //移动view  
  60.     self.center = newcenter;  
  61. }  
  62.   
  63. /*  
  64. // Only override drawRect: if you perform custom drawing.  
  65. // An empty implementation adversely affects performance during animation.  
  66. - (void)drawRect:(CGRect)rect  
  67. {  
  68.     // Drawing code  
  69. }  
  70. */  
  71.   
  72. @end  


效果图






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值