IOS_可移动的UIImageView(随手指移动)
2016-02-29 16:36
399 查看
最近做了2个项目,都是跟UIImageView的图片拖动有关。
其实这个方法很好实现。可以自己子类化一个UIImageView类。然后设置这个子类能够响应交互行为。
再在touches事件中去处理拖动事件,设置新的frame。
示例代码:
先设置
aniImgView.image = img; //aniImgView是UIImageView实例
aniImgView.userInteractionEnabled = YES; //设置响应交互行为,默认是no
再处理touches事件:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
// Retrieve the touch point
beginPoint = [[touches anyObject] locationInView:self]; //记录第一个点,以便计算移动距离
if ([self._delegate respondsToSelector: @selector(animalViewTouchesBegan)]) //设置代理类,
在图像移动的时候,可以做一些处理
[self._delegate animalViewTouchesBegan];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point
// 计算移动距离,并更新图像的frame
CGPoint pt = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += pt.x - beginPoint.x;
frame.origin.y += pt.y - beginPoint.y;
[self setFrame:frame];
if ([self._delegate respondsToSelector: @selector(animalViewTouchesMoved)])
[self._delegate animalViewTouchesMoved];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if ([self._delegate respondsToSelector: @selector(animalViewTouchesEnded)])
[self._delegate animalViewTouchesEnded];
}
通过加入上面的代码,就可以实现图片的拖动了。
其实这个方法很好实现。可以自己子类化一个UIImageView类。然后设置这个子类能够响应交互行为。
再在touches事件中去处理拖动事件,设置新的frame。
示例代码:
先设置
aniImgView.image = img; //aniImgView是UIImageView实例
aniImgView.userInteractionEnabled = YES; //设置响应交互行为,默认是no
再处理touches事件:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
// Retrieve the touch point
beginPoint = [[touches anyObject] locationInView:self]; //记录第一个点,以便计算移动距离
if ([self._delegate respondsToSelector: @selector(animalViewTouchesBegan)]) //设置代理类,
在图像移动的时候,可以做一些处理
[self._delegate animalViewTouchesBegan];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point
// 计算移动距离,并更新图像的frame
CGPoint pt = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += pt.x - beginPoint.x;
frame.origin.y += pt.y - beginPoint.y;
[self setFrame:frame];
if ([self._delegate respondsToSelector: @selector(animalViewTouchesMoved)])
[self._delegate animalViewTouchesMoved];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if ([self._delegate respondsToSelector: @selector(animalViewTouchesEnded)])
[self._delegate animalViewTouchesEnded];
}
通过加入上面的代码,就可以实现图片的拖动了。
相关文章推荐
- Salesforce Process Builder - 进程生成器
- soj 2804 Longest Ordered Subsequence (II) LIS nlogn
- 解决 gradle 始终在Building的问题
- 重新生成索引存储过程 sp_rebuild_index
- duilib : 滑动显示的窗口实现以及 悬浮窗 (转载)
- Arduino - 利用电阻进行分压,测量12V电压
- 管理系统UI: System Bar 详解
- UINavigationController侧滑滑动返回 卡死问题
- 从客户端中检测到有潜在危险的Request.Form值的解决办法
- iOS7 UIKit动力学-碰撞特性UICollisionBehavior 下
- 关于利用drawable 来提升UI Perfermance(启动页时,先显示空白页 )
- libsvm之SVM_GUI使用
- UIKit框架(15)PCH头文件
- Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
- 设置网格UICollectionView的单元格间距
- UIkit框架(14)自定义标签控制器
- uicollectionView
- Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- IOS开发 GCD介绍: 基本概念和Dispatch Queue
- POJ3017Cut the Sequence|DP|单调队列|set