gpt4 book ai didi

ios - 重定向 UIControl 上的拖动事件

转载 作者:行者123 更新时间:2023-11-28 22:52:33 25 4
gpt4 key购买 nike

对于我正在开发的 iOS 应用程序,用户设备上的所有图像都显示在屏幕的一部分上。用户应该能够将这些图像拖到屏幕的另一部分,以将它们添加到将在内存游戏中使用的图像集中。

这是一个屏幕截图,以防我的解释难以形象化 A screenshot to make things more clear

当用户拖动图像时,我希望发生的是创建该图像的稍小的半透明版本,然后将其拖到底部,但原始图像保留在那里。我可以很好地生成新图像,甚至可以指定一个移动新图像的目标。我遇到的问题是为了拖动新图像,用户必须将手指从屏幕上移开并再次开始拖动。我希望新图像接管旧图像拖动事件。这是我现在拥有的代码。

UIControl *mask =  [[UIControl alloc]initWithFrame:frame];
[mask addSubview:img];
[mask addTarget:self action:@selector(dragExited:withEvent:) forControlEvents:UIControlEventTouchDragExit];
[scrImages addSubview:mask];


-(IBAction)dragExited:(id)sender withEvent:(UIEvent*)event
{
UIControl *control = sender;
[control resignFirstResponder];
UIImageView *img = [control.subviews objectAtIndex:0];
CGPoint point = [[[event allTouches]anyObject]locationInView:self.view];
UIControl *mask = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, img.image.size.width, img.image.size.height)];
mask.center = point;
[mask addSubview:[[UIImageView alloc]initWithImage:img.image]];
[mask addTarget:mask action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mask];
[mask addTarget:self action:@selector(thumbnailMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[mask touchesMoved:nil withEvent:nil];
}

-(IBAction)thumbnailMoved:(id)sender withEvent:(UIEvent*)event
{
UIControl *control = sender;
CGPoint point = [[[event allTouches]anyObject]locationInView:self.view];
control.center = point;
}

我试图通过调用 [mask touchesMoved:nil withEvent:nil]; 来启动拖动过程,但这似乎不起作用。有什么想法吗?

最佳答案

我将用不同的技巧来回答这个问题,pan gestures .我没有使用你的技术,所以我不能说,但我一直在使用以下内容:

UIPanGestureRecognizer *pan;

pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
image01.userInteractionEnabled = YES;
[self.image01 addGestureRecognizer:pan];

pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
image02.userInteractionEnabled = YES;
[self.image02 addGestureRecognizer:pan];

然后我有了处理程序:

- (void)handlePanImage:(UIPanGestureRecognizer *)sender
{
static CGPoint originalCenter;
static UIImageView *imageview;

if (sender.state == UIGestureRecognizerStateBegan)
{
UIImageView *selectedImageView = (UIImageView *) sender.view;
imageview = [[UIImageView alloc] initWithFrame:selectedImageView.frame];
imageview.image = selectedImageView.image;
imageview.alpha = 0.8;
[sender.view.superview addSubview:imageview];

originalCenter = imageview.center;
}
else if (sender.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [sender translationInView:sender.view.superview];

imageview.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
[imageview removeFromSuperview];
imageview = nil;

CGPoint translation = [sender translationInView:sender.view.superview];

// do whatever you want to at the end, in this case, animating the moving
// of the old image to the new location. perhaps you don't want to move your
// old image, but leave it there, maybe you just want to change the alpha of
// what you dragged back to 1.0 and snap it into place. Just do whatever you
// want here. Note, in this latter scenario, if you want to be able to drag
// the new image back out, you'll have to create a new pan gesture recognizer
// for it.

[UIView animateWithDuration:0.2
animations:^{
sender.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);;
}];
}
else if (sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
{
[imageview removeFromSuperview];
imageview = nil;
}
}

您也可以不为每个图像创建一个新的手势识别器,只需在父 View 上创建一个,检测您正在处理的图像,然后继续。这可能看起来像:

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanSuperView:)];
[self.view addGestureRecognizer:pan];

然后:

- (void)handlePanSuperView:(UIPanGestureRecognizer *)sender
{
static CGPoint originalCenter;
static UIImageView *selectedImageView = nil;
static UIImageView *draggedImageView = nil;

if (sender.state == UIGestureRecognizerStateBegan)
{
selectedImageView = nil;
CGPoint location = [sender locationInView:sender.view];
for (UIImageView *imageview in _imageviews)
{
if (CGRectContainsPoint(imageview.frame, location))
selectedImageView = imageview;
}
if (selectedImageView)
{
draggedImageView = [[UIImageView alloc] initWithFrame:selectedImageView.frame];
draggedImageView.image = selectedImageView.image;
draggedImageView.alpha = 0.8;
[sender.view addSubview:draggedImageView];

originalCenter = selectedImageView.center;
}
}
else if (sender.state == UIGestureRecognizerStateChanged && selectedImageView)
{
CGPoint translation = [sender translationInView:sender.view];

draggedImageView.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
}
else if (sender.state == UIGestureRecognizerStateEnded && selectedImageView)
{
[draggedImageView removeFromSuperview];
draggedImageView = nil;

CGPoint translation = [sender translationInView:sender.view];

// do whatever you want to at the end, in this case, animating the moving of the old image

[UIView animateWithDuration:0.2
animations:^{
selectedImageView.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);;
}];
}
else if ((sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed) && selectedImageView)
{
[draggedImageView removeFromSuperview];
draggedImageView = nil;
}
}

关于ios - 重定向 UIControl 上的拖动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11587190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com