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

时间:2015-04-26 15:07:50

标签: ios objective-c animation uiview core-animation

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

enter image description here

同样在代码中我将UILabel添加为子视图和更多元素。

如果您能提供代码示例,我们将不胜感激 非常感谢您提供任何支持!

更新:

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

尝试了下一个代码:

<source ng-src="{{ video.url }}" type='video/mp4' /></source>

1 个答案:

答案 0 :(得分:2)

您可以通过将透视和旋转变换应用于其基础CALayer(3D变换)来以此方式为视图设置动画。 要从左侧开始,您可以设置图层的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;
}];