我正在创建一个应用程序,用户可以在其中使用当前图像设计自己的舞台。我已经创建了按钮作为图像,现在我想要一些代码,使用户可以拖放图像(按钮)在特定的图像视图区域。用户应该可以拖动图像并将其放置在ios的图像视图中
0
A
回答
0
您可以通过点击手势(例如点击第一个图像视图,点击目标图像视图)来实现此目的,但这不是很直观。使用UIPanGestureRecognizer进行适当的拖放操作对于最终用户来说会更加直观。
从技术上讲,你不是从图像视图拖动图像,而是实际上拖动图像视图本身。 (一个图像本身没有图像视图,没有可视化表示。)当你放开时,你将动画改变图像视图帧来完成幻觉。
如果你有imageViews的NSArray的,你可以手势添加到他们的上海华:
@property (nonatomic, strong) NSMutableArray *imageViews;
- (void)viewDidLoad
{
[super viewDidLoad];
[self createImageViewArray];
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:gesture];
}
- (void)createImageViewArray
{
self.imageViews = [NSMutableArray array];
}
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
static UIImageView *draggedImage = nil;
static CGRect draggedImageOriginalFrame;
CGPoint location = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateBegan)
{
draggedImage = [self determineImageForLocation:location];
if (draggedImage)
{
draggedImageOriginalFrame = draggedImage.frame;
[draggedImage.superview bringSubviewToFront:draggedImage];
}
}
else if (gesture.state == UIGestureRecognizerStateChanged && draggedImage != nil)
{
CGPoint translation = [gesture translationInView:gesture.view];
CGRect frame = draggedImageOriginalFrame;
frame.origin.x += translation.x;
frame.origin.y += translation.y;
draggedImage.frame = frame;
}
else if (draggedImage != nil && (gesture.state == UIGestureRecognizerStateEnded ||
gesture.state == UIGestureRecognizerStateCancelled ||
gesture.state == UIGestureRecognizerStateFailed))
{
UIImageView *droppedOver = nil;
if (gesture.state == UIGestureRecognizerStateEnded)
droppedOver = [self draggedImageView:draggedImage toLocation:location];
if (droppedOver == nil)
{
[UIView animateWithDuration:0.25
animations:^{
draggedImage.frame = draggedImageOriginalFrame;
}];
}
else
{
[droppedOver.superview bringSubviewToFront:droppedOver];
[UIView animateWithDuration:0.25
animations:^{
draggedImage.frame = droppedOver.frame;
droppedOver.frame = draggedImageOriginalFrame;
}];
}
}
}
- (UIImageView *)draggedImageView:(UIImageView *)draggedView toLocation:(CGPoint)location
{
for (UIImageView *imageview in self.imageViews)
if (CGRectContainsPoint(imageview.frame, location) && imageview != draggedView)
return imageview;
return nil;
}
- (UIImageView *)determineImageForLocation:(CGPoint)location
{
return [self draggedImageView:nil toLocation:location];
}
+0
给他upvote – 2014-08-21 08:50:20
相关问题
- 1. 图像拖放IOS像happn应用
- 2. 用可拖动的图像更改可拖放的图像源
- 3. Java拖放图像并拖动时显示图像缩略图
- 4. 如何缩放图像以适合ios中的图像视图?
- 5. monodroid如何在图像上放置图像并放置图像
- 6. 拖动图像视图
- 7. 缩放放置在图像视图中的图像的控件?
- 8. 如何通过将图像拖放到拖动代理图像位置来放置图像
- 9. 在另一个图像IOS上的可拖动图像Swift
- 10. 如何拖动图像并将其放置在所需的位置?
- 11. 从滚动视图中拖动图像
- 12. 拖放图像
- 13. 拖动并调整图像大小并将此图像放在另一个图像上并合并到PHP
- 14. 用相机捕捉图像并将图像放在该图像上,怎么样?
- 15. 缩放图像并在图像视图中进行设置会降低图像质量并缩小图像
- 16. 查看和滚动视图中的TapRecognizer(在视图中拖放图像)
- 17. 拖放图像可见性
- 18. 图像阴影拖放网格视图?
- 19. 是否可以从iOS中的其他图像剪切图像?
- 20. IOS:拖动多个图像
- 21. 动态创建/绘制图像以放置在Android视图中
- 22. 将图像放入视图中像在Android中的图库
- 23. 在视图中放置图像
- 24. 如何在视图中拖放浮动图像浏览
- 25. 图像在Node.js中拖放并上传
- 26. 我们如何获得图像的位置并将其他图像放置在该位置使用android
- 27. 在Chrome中拖放图像
- 28. 在java中拖放图像
- 29. 如何在iOS中拖动图像时在另一图像上重叠图像?
- 30. 在滚动视图中缩放图像
我新来的发展,这是我的第一个项目。 – user2368693 2013-05-10 06:47:30