Rotate UIImageView around its center point?(围绕其中心点旋转 UIImageView?)
问题描述
我想要围绕其中心点旋转的 UIImageView (self.myImage
) 中有一个透明 png.代码应该很简单:
I have a transparent png inside a UIImageView (self.myImage
) that I want to rotate around its center point.
The code should be pretty simple:
[self.myImage.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
[UIView animateWithDuration:1.0 animations:^{
[self.myImage setTransform:CGAffineTransformMakeRotation(angle)];
}];
图像以正确的速度/时间和正确的角度旋转,但其位置发生了偏移.这是正在发生的事情的一个示例:
The image rotates at the right speed/time and at the right angle, but its position gets shifted. Here's an example of what's happening:
灰色方块只是为了显示在屏幕上的位置.另一个图是透明的 png(包含在 UIImageView 中).白色虚线显示 UIImageView 的中心.图像左侧显示图像的原始位置,右侧显示使用上述代码旋转后的图像(向右移动一点).黑白圆圈位于图像文件的中心.
The gray square is just to show position in the screen. The transparent png (contained in a UIImageView) is the other figure. The white dotted lines show the center of the UIImageView. The left side of the image shows the original position of the image, the right side shows the image after being rotated with the above code (which gets shifted a little down to the right). The black and white circles are in the center of the image file.
我有什么遗漏的吗?据我了解,上面的第一行不是必需的,因为这些是默认值.我是否必须在情节提要中/以编程方式设置/取消设置某些内容?
Is there something that I'm missing? As far as I understand, the first line above is not required because those are the defaults. Do I have to set/unset something in the storyboard/programmatically?
推荐答案
我发现完成我需要的代码比@Albin Joseph 给出的代码更简单,但它确实为我指明了正确的方向.我的动画需要从中断的地方重新开始并旋转到新位置.有时它会被动画化,有时则不会.所以代码如下:
I found out that the code I need to accomplish what I needed was simpler than the one given by @Albin Joseph, but it did point me to the right direction. My animation needs to pick up where it left off and rotate to a new position. Some times it will be animated and sometimes it won't. So hence the code:
CGFloat duration = animated ? 0.5 : 0.01;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [[self.turnIndicatorImage.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"];
animation.toValue = angle;
animation.duration = duration;
animation.fillMode = kCAFillModeForwards;
animation.repeatCount = 0;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[self.turnIndicatorImage.layer addAnimation:animation forKey:@"transform.rotation.z"];
这篇关于围绕其中心点旋转 UIImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:围绕其中心点旋转 UIImageView?
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01