问题描述
我想让IBOutlet围绕父视图中的特定点旋转,目前,我只知道如何围绕锚点旋转它,但是,我想在对象图层之外使用一个点.
I would like to make an IBOutlet rotate around a specific point in the parent view,currently i only know how to rotate it around an anchor point, however, i want to use a point outside the object's layer.
从该点算起相对于设备航向的旋转角度.
The rotation angle is calculated relative to the device heading from the point.
- (void)viewDidLoad{
[super viewDidLoad];
locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.headingFilter = 1;
locationManager.delegate=self;
[locationManager startUpdatingHeading];
[locationManager startUpdatingLocation];
compassImage.layer.anchorPoint=CGPointZero;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
// Convert Degree to Radian and move the needle
float oldRad = -manager.heading.trueHeading * M_PI / 180.0f;
float newRad = -newHeading.trueHeading * M_PI / 180.0f;
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
theAnimation.toValue=[NSNumber numberWithFloat:newRad];
theAnimation.duration = 0.5f;
[compassImage.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
compassImage.transform = CGAffineTransformMakeRotation(newRad);
NSLog(@"%f (%f) => %f (%f)", manager.heading.trueHeading, oldRad, newHeading.trueHeading, newRad);
}
我如何通过alpha围绕(x,y)旋转UIImageView?
推荐答案
要围绕特定点(内部或外部)旋转,您可以更改图层的锚点,然后应用类似于.
For rotating around a specific point (internal or external) you can change the anchor point of the layer and then apply a regular rotation transform animation similar to what I wrote about in this blog post.
您只需要知道锚点也会影响图层在屏幕上的显示位置.当您更改锚点时,还必须更改位置,以使该图层出现在屏幕上的同一位置.
You just need to be aware that the anchor point also affects where the layer appears on the screen. When you change the anchor point you will also have to change the position to make the layer appear in the same place on the screen.
假设该图层已经放置在其起始位置并且已知旋转点,则可以这样计算锚点和位置(请注意,锚点位于该图层的单位坐标空间中边界(意味着x和y的范围在0到1之间):
Assuming that the layer is already placed in it's start position and that the point to rotate around is known, the anchor point and the position can be calculated like this (note that the anchor point is in the unit coordinate space of the layer's bounds (meaning that x and y range from 0 to 1 within the bounds)):
CGPoint rotationPoint = // The point we are rotating around
CGFloat minX = CGRectGetMinX(view.frame);
CGFloat minY = CGRectGetMinY(view.frame);
CGFloat width = CGRectGetWidth(view.frame);
CGFloat height = CGRectGetHeight(view.frame);
CGPoint anchorPoint = CGPointMake((rotationPoint.x-minX)/width,
(rotationPoint.y-minY)/height);
view.layer.anchorPoint = anchorPoint;
view.layer.position = rotationPoint;
然后,您只需对其应用旋转动画,例如:
Then you simply apply a rotation animation to it, for example:
CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotate.toValue = @(-M_PI_2); // The angle we are rotating to
rotate.duration = 1.0;
[view.layer addAnimation:rotate forKey:@"myRotationAnimation"];
请注意,您已经更改了锚点,因此position
不再位于frame
的中心,并且如果您应用其他变换(例如比例),它们也将相对于锚点.
Just note that you have changed the anchor point so the position
is no longer in the center of the frame
and if you apply other transforms (such as a scale) they will also be relative to the anchor point.
这篇关于核心动画围绕点旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!