【发布时间】:2014-11-13 23:59:37
【问题描述】:
我有 5 个UIImageViews 在屏幕上显示动画。如果按下一个,并且它符合要求,它会给你的分数加 1,使用这个:
self.score.text = @(self.score.text.integerValue + 1).stringValue;
但是当UILabel中的文字更新时,所有的动画都会突然停止,剩下的UIImageViews也会消失。但是,动画只在几秒钟后重新开始,就好像图像被隐藏了一样。动画图像和更改图像的代码(每个都相同):
- (void) squareOneMover {
NSUInteger r = arc4random_uniform(3);
[self.squareOne setHidden:NO];
CGPoint originalPosition = self.squareOne.center;
CGPoint position = self.squareOne.center;
originalPosition.y = -55;
position.y += 790;
[self.squareOne setCenter:originalPosition];
[UIView animateWithDuration:r + 3
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.squareOne setCenter:position];
}
completion:^(BOOL complete) {
if (complete) {
[self squareOneColour];
[self squareOneMover];
}
}
];
}
- (void) squareOneColour {
NSUInteger r = arc4random_uniform(5);
[self.squareOne setImage:[self.colorArray objectAtIndex:r]];
}
有人有解决办法吗?如果通过更改 UILabel 中的文本应该停止动画(我不知道他们为什么会这样做),有人可以提供一种解决方法来使保持得分成为可能。
编辑:我创建了一个按钮,可以增加score 的整数值。这意味着我可以手动更改UILabel 中的文本。文本更改时动画仍然停止。
编辑2:按下事件的方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSUInteger a = [self.colorArray indexOfObject:self.squareOne.image];
NSUInteger b = [self.iconColorArray indexOfObject:self.icon.image];
if ([self.squareOne.layer.presentationLayer hitTest:touchLocation]) {
_squareOne.hidden = YES;
}
if (a!=b) {
if (self.squareOne.hidden==YES) {
NSLog(@"NO");
}
}
if (a==b) {
if ([self.squareOne.layer.presentationLayer hitTest:touchLocation]) {
self.score.text = @(self.score.text.integerValue + 1).stringValue;
}
}
if ([self.squareTwo.layer.presentationLayer hitTest:touchLocation]) {
_squareTwo.hidden = YES;
}
}
看看马特的回答,我将如何通过“更改其约束”为UIImageView 设置动画?
约束:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-25-[btn1]-25-[btn2(==btn1)]-25-[btn3(==btn1)]-25-[btn4(==btn1)]-25-[btn5(==btn1)]-25-|" options:0 metrics:nil views:views]];
[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.squareOne
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:0.0
constant:-55.0]];
【问题讨论】:
-
我认为问题在于按下事件。你能告诉我们这样做的方法吗?
标签: objective-c animation uiimageview uitextfield