iphone – 拖放对象并将其返回到原始位置

iphone – 拖放对象并将其返回到原始位置,第1张

概述我试图拖动一个图像,让它在释放后返回原来的位置.到目前为止,我可以使用以下代码将图像创建为按钮来拖动图像:(如此问题的答案中所示: Basic Drag and Drop in iOS) UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];[button addTarget:self action:@selector(i 我试图拖动一个图像,让它在释放后返回原来的位置.到目前为止,我可以使用以下代码将图像创建为按钮来拖动图像:(如此问题的答案中所示: Basic Drag and Drop in iOS)

UIbutton *button = [UIbutton buttonWithType:UIbuttonTypeCustom];[button addTarget:self action:@selector(imagetouch:withEvent:) forControlEvents:UIControlEventtouchDown];[button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventtouchDragInsIDe];[button setimage:[UIImage imagenamed:@"vehicle.png"] forState:UIControlStatenormal];[self.vIEw addSubvIEw:button];

后来我定义:

- (IBAction) imageMoved:(ID) sender withEvent:(UIEvent *) event{    CGPoint point = [[[event alltouches] anyObject] locationInVIEw:self.vIEw];    UIControl *control = sender;    control.center = point;}

如何在释放后将其恢复到原始位置?对于初学者我会保存每个图像应该返回的位置,但是,无论如何,无论如何都要指示UIImage从它的当前位置移动并移动到另一个?或任何其他替代解决方案?

解决方法 你知道吗,我昨天刚刚开始研究这个问题,所以在这里你会得到一些奖励动画:

- (voID)prevIEwImagetouchUpInsIDe:(UIbutton*)abutton {    if (!prevIEwImageDragged) {        [self selectPrevIEwImage:abutton];        return;}    prevIEwImageDragged = NO;    // If user drag photo a little and then release,we'll still treat it as a tap    //  instead of drag    if ((originalpositionOfbutton.x - abutton.center.x) *       (originalpositionOfbutton.x - abutton.center.x) +       (originalpositionOfbutton.y - abutton.center.y) *       (originalpositionOfbutton.y - abutton.center.y) < 10) {        abutton.center = originalpositionOfbutton;        [self selectPrevIEwImage:abutton];        return;    }    [UIVIEw animateWithDuration:0.5        delay:0        options:UIVIEwAnimationoptionCurveEaSEOut |        UIVIEwAnimationoptionAllowUserInteraction animations:^{        abutton.center = originalpositionOfbutton;    } completion:nil];}- (voID)prevIEwImageDraggedInsIDe:(UIbutton*)abutton event:(UIEvent*)event {    if (!prevIEwImageDragged) {        originalpositionOfbutton = abutton.center;        [self.vIEw bringSubvIEwToFront:abutton];        [self.vIEw bringSubvIEwToFront:[self.vIEw vIEwWithTag:CHOOSE_PHOTO_button_TAG]];    }    UItouch* touch = [[event alltouches] anyObject];    prevIEwImageDragged = YES;    abutton.center = CGPointMake(abutton.center.x+[touch locationInVIEw:self.vIEw].x-        [touch prevIoUsLocationInVIEw:self.vIEw].x,abutton.center.y+[touch locationInVIEw:self.vIEw].y-        [touch prevIoUsLocationInVIEw:self.vIEw].y);}

它仍然有一些神奇的数字,我没有重命名函数和变量以适合你的例子,但它应该是清楚的.我也在这里做了缩进,因为我没有在我的代码中手动断行长行.

总结

以上是内存溢出为你收集整理的iphone – 拖放对象并将其返回到原始位置全部内容,希望文章能够帮你解决iphone – 拖放对象并将其返回到原始位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1025039.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存