0
我有一个应用程序,我需要重现骰子动画。每次掷骰子时,我都应该改变他在X轴上的位置和他的旋转。同时旋转和改变视图位置?
骰子是一个UIButton
内UIView
与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轴值),骰子保持相同的大小。
我已经在网上搜索了很多,但对我没有任何用处。任何人有一个想法,请?非常感谢!