绕底轴旋转 UIImageView - Objective-C/iOS

发布于 2024-12-06 08:25:12 字数 1305 浏览 0 评论 0原文

我有一个 UIImage 视图,我想像翻转时钟一样翻转为平面。这是水平面的第一部分(我将在其中更改图像或添加新视图或其他内容)。我的问题是让视图在底部轴上翻转。它是 100px 的正方形。我怎样才能让它按底部轴旋转。我读过很多堆栈问题和答案,用谷歌搜索,但我的答案都不起作用。这是我最接近的,它似乎通过在视图翻转之前移动视图来翻转,但我无法让它翻转但保持静止。

查看 http://www.voyce.com/index.php/2010/04/10/creating-an-ipad-flip-clock-with-core-animation/ 它将似乎我的轴是正确的,但我显然不正确!

[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 有一个高度,因为偏移量似乎与它的高度相同!

I have a UIImage view that I want to flip to being flat like a flip clock does. This is the first part to the horizontal plane (where I will change the image or add a new view or something). My problem is getting the view to flip on the bottom axis. it's 100px square. How can I get it to rotate by the bottom axis. I have read many many stack problems and answers, googled it and what ever answers I have don't work. This is the closest I have which seems to flip by moving the view before it does so and I can't get it to flip but stay still.

Looking at the Applying the Animation section of http://www.voyce.com/index.php/2010/04/10/creating-an-ipad-flip-clock-with-core-animation/ it would seem that I have the axis correct, but I clearly don't!

[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 is a UIImageView

What do I need to add to get it to flip in the right way? Been trying to do this for 2 days now!

EDIT:

I allocated the UIImageView with this:

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

EDIT 2:

I have discovered that if I create my UIImageView when I click (not what I want to do) after the view is loaded, it rotates where I want it too. I think that it might be due to the UINavigationController having a height, as the offset seems to be the same as it's height!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2024-12-13 08:25:12

你的代码中有一些错误。

首先我不知道为什么将 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) {
                 }];

You've got few things wrong in your code.

First I have no idea why do you set boxView.center to (0.5, 1). Second you shouldn't set anchor point inside animation block and third M_PI_2 is just half animation you really want.

Here is the solution. I used UILabel instead of 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) {
                 }];
め可乐爱微笑 2024-12-13 08:25:12

我实际上根据语音教程制作了一个小框架。也许这有帮助:

https://github.com/jaydee3/JDFlipNumberView

它包含三个类:

  • JDFlipNumberView(单个动画数字)
  • JDGroupedFlipNumberView(分组和链接的可选择数量的翻转视图,以获得更大的数字)
  • JDDateCountdownFlipView(日期倒计时,只需用日期初始化,设置框架即可。)

无论如何,您只需要执行三个步骤:

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

I actually made a small framework out of the voice tutorial. Perhaps that helps:

https://github.com/jaydee3/JDFlipNumberView

It contains three classes:

  • JDFlipNumberView (a single animated digit)
  • JDGroupedFlipNumberView (a grouped and chained choosable number of flipviews for higher numbers)
  • JDDateCountdownFlipView (a date countdown, just init with a date, set a frame and there you go.)

In any case, you just need to do three steps:

  1. Init the class
  2. Set an int value (or a date)
  3. Start the animation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文