CATransform3d 一个 UIButton

发布于 2024-10-20 00:23:36 字数 1456 浏览 0 评论 0原文

我似乎无法在按钮上进行简单的 3D 转换,并且文档似乎没有帮助。我从基于视图的应用程序模板开始。我添加了 QuartzCore 框架并将其导入到 ViewController 标头中。在界面生成器中,我拖出两个圆形矩形按钮并将它们分别连接到button1和button2,并将它们连接到buttonPressed:方法。

快速运行注释掉的动画可以验证只有单击的按钮才会更改其标签。

动画恢复后,我收到错误 Incomplete type for argument 1 of 'setTransform:' on the "sender.transform = t;"线(现在固定线更改为 [sender.layer setTransform:t];)

我如何让它旋转 360 度。我认为问题在于开始和结束状态是相同的,所以它不知道在任何地方旋转。

这是 .h(由于格式原因,我在本文中省略了导入)

@interface animation2ViewController : UIViewController {

    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;

}

-(IBAction) buttonPressed:(UIButton *)sender;

@end

和 .m 文件

#import "animation2ViewController.h"

@implementation animation2ViewController

-(IBAction)buttonPressed:(UIButton *)sender {

    [sender setTitle:@"new title" forState:UIControlStateNormal];
    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         CATransform3D t = CATransform3DIdentity;
                         t.m34 = -1 / 1000;
                         t = CATransform3DRotate(t, 360 * M_PI / 180, 0.0, 1.0, 0.0);  //ultimately want 360 degrees but starting here first
                         [sender.layer setTransform:t];
                     } 
                     completion:^(BOOL finished) {

                     }];

}

I can't seem to get a simple 3d transform to work on a button and the documentation doesn't seem to be helping. I started with a View-based application template. I add in the QuartzCore framework and import it in the ViewController header. In interface builder I drag out two Round Rect Buttons and wire them up to button1, and button2 respectively, and also wire them both to the buttonPressed: method.

A quick run with the animation commented out verifies that only the button that is clicked changes its label.

With the animation back in I get an error Incompatible type for argument 1 of 'setTransform:' on the "sender.transform = t;" line (This is now fixed line changed to [sender.layer setTransform:t];)

How do I get it to rotate 360 degrees. I think the problem is that the start and end states are the same so it doesn't know to rotate through anywhere.

Here is the .h, (i left out the imports for formatting reasons in this post)

@interface animation2ViewController : UIViewController {

    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;

}

-(IBAction) buttonPressed:(UIButton *)sender;

@end

And the .m file

#import "animation2ViewController.h"

@implementation animation2ViewController

-(IBAction)buttonPressed:(UIButton *)sender {

    [sender setTitle:@"new title" forState:UIControlStateNormal];
    [UIView animateWithDuration:1.0
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         CATransform3D t = CATransform3DIdentity;
                         t.m34 = -1 / 1000;
                         t = CATransform3DRotate(t, 360 * M_PI / 180, 0.0, 1.0, 0.0);  //ultimately want 360 degrees but starting here first
                         [sender.layer setTransform:t];
                     } 
                     completion:^(BOOL finished) {

                     }];

}

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

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

发布评论

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

评论(2

记忆で 2024-10-27 00:23:36

M_PI 是 180° 旋转,因此这意味着您旋转 360° 90 次。
尝试 t = CATransform3DRotate(t, M_PI, 0.0, 1.0, 0.0);
对于设置器错误,请尝试 [sender.layer setTransform:t];
因为你可以只变换图层

最后你需要在最后提交动画,否则它不会动画
[UIView 提交动画];

M_PI is a rotation of 180°, so that means you do 90 times a 360°.
try t = CATransform3DRotate(t, M_PI, 0.0, 1.0, 0.0);
and for the setter error try [sender.layer setTransform:t];
because you can just transform the layer

And for the end you need to commit the animation at the end, otherwise it won't animate
[UIView commitAnimations];

守不住的情 2024-10-27 00:23:36

试试这个:

  1. 旋转 1/3 完整旋转
  2. 做动画

你可能已经弄清楚了,因为你发布的帖子是一年多前的,但对于任何有同样问题的人来说,请尝试这样做它应该有效。

Try this :

  1. Rotate by 1/3 of a full rotation
  2. Do the animations

You have probably figured it out by now seeing as you post was over a year ago but for anyone with the same question try it like that it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文