gpt4 book ai didi

ios - 如何使用 UIView 创建自定义翻转动画

转载 作者:行者123 更新时间:2023-11-28 21:46:45 26 4
gpt4 key购买 nike

我正在尝试创建一个自定义动画,类似于 UIView 的默认翻转动画,但没有实际的翻转,而且我希望翻转的轴位于左侧而不是中心。这是我要完成但没有重复的示例:

enter image description here

同样在代码中,我将添加 UILabel 作为 subview 和一些其他元素。

如能提供代码示例,将不胜感激非常感谢您的支持!

更新:

使用@artal 提供的示例并为 UIImageView 获得了想要的效果,但是如何为 UIButton 获得相同的示例?

尝试下一个代码:

objectivesButton = [[UIButton alloc] initWithFrame:CGRectMake( 0.25f * [UIScreen mainScreen].bounds.size.width, 0.7f * [UIScreen mainScreen].bounds.size.height, 0.5f * [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.3f)];
[objectivesButton addTarget:self
action:@selector(startAnimationButton)
forControlEvents:UIControlEventTouchUpInside];
objectivesButton.backgroundColor = [UIColor yellowColor];
objectivesButton.layer.anchorPoint = CGPointMake(0, 0.5);
[self.view addSubview:objectivesButton];

- (void)startAnimationButton{

CATransform3D perspectiveTrasnform = CATransform3DIdentity;
perspectiveTrasnform.m34 = -0.004;

CGFloat rotateAngleDeg = 90;
CATransform3D flipTransform = CATransform3DRotate(perspectiveTrasnform, rotateAngleDeg / 180.0 * M_PI, 0, 1.5, 0);
[UIView animateWithDuration:3.0f animations:^()
{
objectivesButton.layer.transform = flipTransform;
}];

}

最佳答案

您可以通过将透视和旋转变换应用于其底层 CALayer(3D 变换),以这种方式为 View 设置动画。要从左侧开始,您可以设置图层的 anchorPoint。这是一个例子:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
imageView.center = CGPointMake(self.view.center.x - imageView.frame.size.width * 0.5, self.view.center.y + imageView.frame.size.height * 0.5);
imageView.layer.anchorPoint = CGPointMake(0, 1);
[self.view addSubview:imageView];

CATransform3D perspectiveTrasnform = CATransform3DIdentity;
perspectiveTrasnform.m34 = -0.004;

CGFloat rotateAngleDeg = 90;
CATransform3D flipTransform = CATransform3DRotate(perspectiveTrasnform, rotateAngleDeg / 180.0 * M_PI, 0, 1, 0);
[UIView animateWithDuration:5 animations:^()
{
imageView.layer.transform = flipTransform;
}];

关于ios - 如何使用 UIView 创建自定义翻转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29879314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com