在两点之间移动UIButton的正确方法? [英] Correct Way To Move UIButton Between Two Points?

查看:58
本文介绍了在两点之间移动UIButton的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试制作一个小型点击游戏,在这里您有一个正方形的网格,需要以不同的顺序点击它们.有时这些正方形会彼此交换位置.

Trying to make a small tapping game, where you have a grid of squares and need to tap them in different orders. Occasionally these squares will swap places with each other.

我认为我需要将每个正方形的位置放到一个数组中.然后,当需要移动时,从当前位置移至从数组随机选择的位置,然后删除数组中的该位置,以免在同一位置出现多个按钮.

I figure that I will need to put the locations of each square into an array. Then when it is time to move, go from the current location to a one selected at random from the array, and then deleting that location in the array so I don't get multiple buttons in the same place.

不幸的是,我什至无法移动UIButton,更不用说在两个位置之间进行插值了.

Unfortunately I can't even get the UIButtons to move, let alone interpolate between two positions.

当前,这将移动一个按钮:

Currently this will move a button:

-(IBAction)button:(id)sender
{
    button.center = CGPointMake(200, 200);
}

但是我不希望它那样工作,我不希望它那样工作,

But I don't want it to work like that, I want it to work something like this and it doesn't work:

if (allButtonsPressed == YES)
{
    button.center = CGPointMake(200, 200);
}

如果这样放置,它甚至不会移动:

It won't even move if placed like this:

-(void)viewDidLoad
{
    [super viewDidLoad];
    button.center = CGPointMake(200, 200);
}

为清楚起见,该按钮是通过Interface Builder添加的,所有这些情况在执行其他操作时都有效,因此我猜想UIButton需要以特定方式移动/动画化吗?

For clarity, the button is added through Interface Builder, and all these situations work when doing other things, so I'm guessing UIButtons need to moved/animated in specific ways?

推荐答案

viewDidLoad在视图可见之前发生,因此该视图将不进行动画处理.您可以尝试将其放在viewDidAppear方法中.

viewDidLoad occurs before the view is visible so it will move just not animated. You could try putting it in the viewDidAppear method.

您可以将内容包装在UIView动画块中,例如,如果您希望它们进行动画处理而不是眨眼到位.

You can wrap things in UIView animation blocks like so if you want them to animate instead of blink into position.

[UIView animateWithDuration:0.5 animations:^{
    button.center = CGPointMake(200.0, 200.0);
}];

由于您使用的是界面生成器,因此我假设您使用using属性来跟踪按钮.如果您只使用了几个设置按钮,但我希​​望在游戏中这些按钮会改变,因此可以使用代码或至少使用IBOutletCollections来跟踪按钮.这可以奏效.

Since you're using interface builder I'm assuming your using properties to keep track of the buttons. This can work if you're only using a few set buttons but I'd expect in a game those will change so you may want to move to code or atleast use IBOutletCollections to keep track of the buttons.

Swift 3.0

UIView.animate(withDuration: 0.5) { [weak self] in
    self?.button.center = CGPoint(x: 200, y: 200)
}

这篇关于在两点之间移动UIButton的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆