iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 核心动画围绕点旋转大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想让IBOutlet围绕父视图中的特定点旋转,
目前我只知道如何围绕一个锚点旋转它,但是,我想使用对象层外的一个点.

相对于从该点前进的设备计算旋转角度.

- (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旋转UIImageView(x,y)?

解决方法

对于围绕特定点(内部或外部)旋转,您可以更改图层的锚点,然后应用类似于 what I wrote about in this blog post的常规旋转变换动画.

您只需要知道锚点也会影响图层在屏幕上的显示位置.更改定位点时,还必须更改位置以使图层显示在屏幕上的相同位置.

假设图层已经放置在它的起始位置并且周围的旋转点已知,则可以像这样计算锚点和位置(注意锚点位于图层边界的单位坐标空间中(意思是x和y在范围内的范围从0到1)):

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;

然后,您只需对其应用旋转动画,例如:

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"];

请注意,您已更改了锚点,因此位置不再位于框架的中心,如果应用其他变换(例如比例),它们也将相对于锚点.

大佬总结

以上是大佬教程为你收集整理的ios – 核心动画围绕点旋转全部内容,希望文章能够帮你解决ios – 核心动画围绕点旋转所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: