2011-09-23 88 views
1

我有要翻转呈扁平状倒装时钟确实一个UIImage视图。这是水平面的第一部分(我将更改图像或添加新视图或其他东西)。我的问题是让视图在底部轴上翻转。它是100px的正方形。我怎样才能让它通过底部轴旋转。我已经阅读了许多堆栈问题和答案,使用了它,以及我曾经无法解答的问题。这是我看到的最接近我的动作,它在动画之前移动了视图,我无法让它翻转但保持静止。在应用的http://www.voyce.com/index.php/2010/04/10/creating-an-ipad-flip-clock-with-core-animation/动画节旋转围绕底部轴线一个UIImageView - 目标C /的iOS

看又好像我有轴正确的,但我显然不!

[UIView beginAnimations:nil context:nil]; 

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:1]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 

boxView.center = CGPointMake(0.5, 1); 

boxViewImage.layer.anchorPoint = CGPointMake(0.5, 1); 
boxViewImage.layer.transform = CATransform3DMakeRotation(M_PI_2,-1.0,0,0); 

[UIView commitAnimations]; 

boxViewImage是一个UIImageView

我需要什么添加到代码能在正确的方式翻转?一直试图做2天吧!

编辑:

我分配的UIImageView这一点:

CGRect frameRect = CGRectMake(0, 0, 100,100); 
boxViewImage = [[UIImageView alloc] initWithFrame:frameRect]; 
boxViewImage.image = [UIImage imageNamed:@"1.png"]; 
[self.view addSubview:boxViewImage]; 

编辑2:

我发现,如果我创造我的UIImageView当我点击(不是我想做的事)在视图加载后,它也旋转到我想要的位置。我认为这可能是由于UINavigationController具有高度,因为偏移似乎与它的高度相同!

回答

5

你有几件事错在你的代码。

首先,我不知道你为什么要设置boxView.center到(0.5:1)。其次,您不应该在动画块中设置锚点,第三个M_PI_2只是您真正想要的一半动画。

这里是解决方案。我使用UILabel而不是UIImageView。

UILabel *testLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 100)] autorelease]; 
testLabel.backgroundColor = [UIColor blackColor]; 
testLabel.text = @"Test"; 
testLabel.textColor = [UIColor whiteColor]; 
[self.view addSubview:testLabel]; 
testLabel.layer.anchorPoint = CGPointMake(0.5, 1);  

[UIView animateWithDuration:1.0 
         delay:0.0 
        options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction 
       animations:^{ 
        testLabel.layer.transform = CATransform3DMakeRotation(M_PI, -1, 0, 0);       
       } 
       completion:^(BOOL finished) { 
       }]; 
+0

谢谢,这已经开始清理的东西了,但由于某种原因,它似乎仍然旋转阿博距离基线20%/ 20px。导航控制器会对坐标有什么影响吗? – Designer023

+0

ps我正在使用M_PI_2,因为我要在高点交换图像。也许这不是最好的方法! – Designer023

+0

哦,我明白了。 20%是一个谜。我不知道它为什么这样做。我发送的代码在我的项目中工作得很好。 – Split

0

我实际上是用语音教程做了一个小框架。也许这可以帮助:

https://github.com/jaydee3/JDFlipNumberView

它包含三类:

  • JDFlipNumberView(单个动画数字)
  • JDGroupedFlipNumberView(分组及链接flipviews的可选择的数量较高的数字)
  • JDDateCountdownFlipView(日期倒计时,只是初始化一个日期,设置一个框架,你去了。)

在任何情况下,你只需要做三个步骤:

  1. 初始化类
  2. 设置一个int值(或日期)
  3. 启动动画