有些日子没更新日志了。
最近做了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];
}
通过加入上面的代码,就可以实现图片的拖动了。
转自: http://hi.baidu.com/bunsman/item/360611effe90823e87d9dec2
本文介绍如何在 iOS 应用中实现 UIImageView 的图片拖动功能。通过子类化 UIImageView 并设置其可响应用户交互,利用 touches 事件处理拖动操作。文中提供了具体的代码示例。
&spm=1001.2101.3001.5002&articleId=8551352&d=1&t=3&u=a92db6ec9c0640d1a339d8ac118bea7a)
1445

被折叠的 条评论
为什么被折叠?



