当我在swift中单击图像时,将UIButtons移回

时间:2016-03-01 13:56:41

标签: ios swift

当我点击图像时,我需要在视图内移动UIButtons,当我再次点击同一图像时,需要将UIButtons移出屏幕。

当我点击UIImage时,4个UIButton在屏幕内移动,代码如下。

    func imageTapped(img: AnyObject)
        {UIView.animateWithDuration(0.5,
        delay: 0,
        options: [.CurveEaseInOut, .AllowUserInteraction],
        animations: {
            self.imageReceivedTrashCan.center = CGPoint(x: 30, y: 530)
            self.imageReceivedRightArrow.center = CGPoint(x: 285, y: 530)
            self.imageReceivedForward.center = CGPoint(x: 160, y: 530)
            self.imageReceivedCross.center = CGPoint(x: 30, y: 30)
        },
        completion: { finished in
            print("Objects moved!")   
    })
}

但是,当我再次点击同一图像时,我找不到如何将UIButtons移回原始位置的方法。感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

即使这看起来真的是老派,没有任何自动布局,我随机猜测你不会改变其他地方的中心和所有中心的立即:

func imageTapped(img: AnyObject) {
   if (self.imageReceivedTrashCan.center == CGPoint(x: 30, y: 530)) {
      UIView.animateWithDuration(0.5,
        delay: 0,
        options: [.CurveEaseInOut, .AllowUserInteraction],
        animations: {
            self.imageReceivedTrashCan.center = CGPoint(x: -30, y: -530)
            self.imageReceivedRightArrow.center = CGPoint(x: -285, y: -530)
            self.imageReceivedForward.center = CGPoint(x: -160, y: -530)
            self.imageReceivedCross.center = CGPoint(x: -30, y: -30)
        },
        completion: { finished in
            print("Objects moved!")   
    })
} else {
UIView.animateWithDuration(0.5,
        delay: 0,
        options: [.CurveEaseInOut, .AllowUserInteraction],
        animations: {
            self.imageReceivedTrashCan.center = CGPoint(x: 30, y: 530)
            self.imageReceivedRightArrow.center = CGPoint(x: 285, y: 530)
            self.imageReceivedForward.center = CGPoint(x: 160, y: 530)
            self.imageReceivedCross.center = CGPoint(x: 30, y: 30)
        },
        completion: { finished in
            print("Objects moved!")   
    })
}
}

答案 1 :(得分:0)

  • 首先,回答你的问题:

您可以保存每个按钮的初始位置,然后在您拨打animateWithDuration:delay:options:animations:completion:的第二个点按,并将位置设置为self.imageReceivedTrashCan.center = initialPosition

您也可以尝试CoreGraphics的翻译方法。

UIView.animateWithDuration(0.5,
        delay: 0,
        options: [.CurveEaseInOut, .AllowUserInteraction],
        animations: {
                 self.imageReceivedTrashCan.transform = CGAffineTransformMakeTranslation(300, 0)
                 ....
},
        completion: { finished in
            print("Objects moved!")   
    })
}

然后在第二次点击时返回到身份转换

UIView.animateWithDuration(0.5,
        delay: 0,
        options: [.CurveEaseInOut, .AllowUserInteraction],
        animations: {
                 self.imageReceivedTrashCan.transform = CGAffineTransformIdentity
                 ....
},
        completion: { finished in
            print("Objects moved!")   
    })
}
  • 但是这个方法是硬编码的,你应该使用autoLayout来制作这种动画。 Look at here

希望这能帮到你;)

答案 2 :(得分:0)

选择imageView,转到属性检查器和检查用户交互已启用

UITapGestureRecognizer *TapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moveMyView:)];
[imageview addGestureRecognizer: TapRecognizer];

- (void) moveMyView:(UITapGestureRecognizer*)sender {

[UIView animateWithDuration:1.0 delay:0.0 options:
UIViewAnimationOptionCurveEaseIn animations:^{
         self.imageReceivedTrashCan.center = CGPoint(x: -30, y: -530)
        self.imageReceivedRightArrow.center = CGPoint(x: -285, y: -530)
        self.imageReceivedForward.center = CGPoint(x: -160, y: -530)
        self.imageReceivedCross.center = CGPoint(x: -30, y: -30)
    } completion:^ (BOOL completed) {}];
}]; 

}