我有一个 View Controller ,其中有一个默认的 UIview 放置在整个屏幕上。我在此 View 中添加了几个 UIImageView(大约 20 个)。我想支持在这些 UIImageView 之间交换图像。我已经使用 UITapGestureRecogniser 来处理这些 UIImageView 上发生的触摸。我可以在 UITapGestureRecogniser 事件本身下处理这些 UIImageView 之间的图像拖放吗? (或)我需要使用 UIPanGestureRecognizer 或触摸事件吗?实现这些UIImageView之间的图像拖放的最简单、最容易的方法是什么?请建议?
谢谢!
最佳答案
一些想法。
您可以通过点击手势来实现此目的(例如点击第一个 ImageView ,点击目标 ImageView ),但这不是很直观。对于最终用户来说,使用
UIPanGestureRecognizer
进行适当的拖放操作会更加直观。从技术上讲,您并不是从 ImageView 中拖动图像,而是实际上拖动 ImageView 本身。 (如果没有 ImageView ,图像本身就没有视觉表示。)当您放手时,您将为 ImageView 帧的变化设置动画以完成幻觉。
如果您有一个 imageViews
的 NSArray,您可以向其 super View 添加手势:
@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];
// add your imageviews to the array any way you want
...
}
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
static UIImageView *draggedImage = nil;
static CGRect draggedImageOriginalFrame;
CGPoint location = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateBegan)
{
// see if we grabbed an imageview
draggedImage = [self determineImageForLocation:location];
// if so, save its old location for future reference and bring it to the front
if (draggedImage)
{
draggedImageOriginalFrame = draggedImage.frame;
[draggedImage.superview bringSubviewToFront:draggedImage];
}
}
else if (gesture.state == UIGestureRecognizerStateChanged && draggedImage != nil)
{
// if we're dragging it, then update its location
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))
{
// if we let go, let's see if we dropped it over another image view
UIImageView *droppedOver = nil;
if (gesture.state == UIGestureRecognizerStateEnded)
droppedOver = [self draggedImageView:draggedImage toLocation:location];
if (droppedOver == nil)
{
// fail; animate the restoring of the view back to it's original position
[UIView animateWithDuration:0.25
animations:^{
draggedImage.frame = draggedImageOriginalFrame;
}];
}
else
{
// succeed; make sure to bring the view we're about to animate to the front
[droppedOver.superview bringSubviewToFront:droppedOver];
// animate the swapping of the views
[UIView animateWithDuration:0.25
animations:^{
draggedImage.frame = droppedOver.frame;
droppedOver.frame = draggedImageOriginalFrame;
}];
}
}
}
// see if we dragged an imageview over one of our imageviews, returning a point
// to the image view we dropped it over
- (UIImageView *)draggedImageView:(UIImageView *)draggedView toLocation:(CGPoint)location
{
for (UIImageView *imageview in self.imageViews)
if (CGRectContainsPoint(imageview.frame, location) && imageview != draggedView)
return imageview;
return nil;
}
// see if we started the gesture over an imageview
// (turns out that this is the same as "did we drag an image over another",
// but as we're not dragging an image yet, just pass nil)
- (UIImageView *)determineImageForLocation:(CGPoint)location
{
return [self draggedImageView:nil toLocation:location];
}
关于iOS:在 UIImageView 之间移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14965910/