2017-02-20 59 views
0

我有一个应用程序,我需要重现骰子动画。每次掷骰子时,我都应该改变他在X轴上的位置和他的旋转。同时旋转和改变视图位置?

骰子是一个UIButtonUIView与16个像素更大!当用户滚动时,我创建了一个用于旋转的动画,同时我为X视图值的变化设置了动画。当动画完成时,我尝试修复一个随机停止位置。

看看我的代码,以更好地理解。

这是我的旋转动画:

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
animation.fromValue = [NSNumber numberWithFloat:0.0f]; 
animation.toValue = [NSNumber numberWithFloat: 2*M_PI]; 
animation.duration = 0.2; 
animation.repeatCount = 2; 
[dice.layer addAnimation:animation forKey:@"SpinAnimation"]; 

int min = -20; 
int maxim = 20; 
int rndValue = min + arc4random() % (maxim - min); 

// Here I change x value; 
CGRect rct = dice.superview.frame; 
rct.origin.x = n + rndValue; 
rct.size.width = dice.superview.bounds.size.width; 
rct.size.height = dice.superview.bounds.size.height; 
[UIView animateWithDuration:0.4 animations:^{ 
    dice.superview.frame = rct; 
}completion:^(BOOL finished){ 
    //Here, after the animation is done, i want to put a random rotation value. 
    float degrees = arc4random() % 30; 
    dice.superview.layer.anchorPoint = CGPointMake(0.5, 0.5); 
    dice.superview.transform = CGAffineTransformMakeRotation(degrees * M_PI/180); 
}]; 

我的问题是不是每次(预计第一个,当我摇)骰子(意见太)变小,因为视图框的大小正在改变,我可以没有发现问题或任何其他解决方案。 PS:如果我只使用其中的一种(旋转或改变X轴值),骰子保持相同的大小。

我已经在网上搜索了很多,但对我没有任何用处。任何人有一个想法,请?非常感谢!

回答

0

我终于找到了我的问题的答案,我在这里发布我的解决方案,也许其他人会遇到同样的问题。

问题由骰子移动而成。 当我移动骰子时,我改变了它的框架,那是我的错误。如果你将改变骰子

替换为帧动画改变的中心点它解决了这个问题:

[UIView animateWithDuration:0.4 animations:^{ 
    dice.superview.center = CGPointMake((self.view.frame.size.width - CGRectGetMaxX(imageBack.frame))/2 + rndValue + CGRectGetMaxX(imageBack.frame), dice.superview.center.y); 
}completion:^(BOOL finished){ 

}]; 

反正有我的价值观..你可以把任何你想要有!

希望它有帮助