简体   繁体   English

仅使用CGAffineTransform旋转而不移动?

[英]Using CGAffineTransform only to rotate and not to move?

i have an app contains a needle that points toward a specific location wherever you're in the world, but when i use the code below it works but the needle both rotates and moves, so how to stop it from moving while rotating ? 我有一个应用程序包含了向一个特定的位置,无论你在世界上是指向针,但是当我使用下面的代码,它的工作原理,但针都旋转移动,因此如何从边旋转移动停止吗?

CLLocationCoordinate2D  currentLocation;
CLLocationDirection     currentHeading;
CLLocationDirection     cityHeading;
#define toRad(X) (X*M_PI/180.0)
#define toDeg(X) (X*180.0/M_PI)
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f

- (void)viewDidLoad
{
    [super viewDidLoad];
    currentHeading=0.0;




    //  isArabic=[starter isArabic];


    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;



    if([CLLocationManager locationServicesEnabled]){
        [locationManager startUpdatingLocation];
    }
    [locationManager startUpdatingHeading];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    currentLocation = newLocation.coordinate;

    [self updateHeadingDisplays];
}
- (void)locationManager:(CLLocationManager *)manager
       didUpdateHeading:(CLHeading *)newHeading{


    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;
    [compass.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
    compass.transform = CGAffineTransformMakeRotation(newRad);

    NSLog(@"%f (%f) => %f (%f)", manager.heading.trueHeading, oldRad, newHeading.trueHeading, newRad);
    // if (newHeading.headingAccuracy < 0)
    //    return;

    // Use the true heading if it is valid.
    CLLocationDirection  theHeading = ((newHeading.trueHeading > 0) ?
                                       newHeading.trueHeading : newHeading.magneticHeading);


    cityHeading = [self directionFrom:currentLocation ];


    currentHeading = theHeading;

    [self updateHeadingDisplays];



}
- (void)updateHeadingDisplays {
    // Animate Compass


    [UIView     animateWithDuration:0.6
                              delay:0.0
                            options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
                         animations:^{
                             CGAffineTransform headingRotation = CGAffineTransformRotate(CGAffineTransformMakeTranslation(0, 0), (CGFloat)toRad(cityHeading)-toRad(currentHeading));

                             needle.transform = headingRotation;
                         }
                         completion:^(BOOL finished) {

                         }];



}

-(CLLocationDirection) directionFrom: (CLLocationCoordinate2D) startPt  {

    double lat1 = toRad(startPt.latitude);
    double lat2 = toRad(21.4266700 );
    double lon1 = toRad(startPt.longitude);
    double lon2 = toRad(39.8261100);
    double dLon = (lon2-lon1);

    double y = sin(dLon) * cos(lat2);
    double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    double brng = toDeg(atan2(y, x));

    brng = (brng+360);
    brng = (brng>360)? (brng-360) : brng;

    return brng;

}

In your screenshot from your comment it looks like you're using Auto Layout (as you should be). 在注释中的屏幕快照中,您似乎正在使用“自动布局”(应该如此)。 But with Auto Layout comes certain behavior. 但是,随着自动版式的出现,某些行为会发生。 You can see the warning from interface builder in the size inspector: 您可以在大小检查器中从界面生成器中看到警告:

The selected views have no constraints. 选定的视图没有约束。 At build time explicit left, top, width and height constraints will be generated for the view. 在构建时,将为视图生成显式的左,上,宽和高约束。

Who knows what constraints are being applied to your view by default! 谁知道默认情况下对您的视图应用了哪些约束! If you constrain the center of your image view to be in a specific spot, the compass image should rotate around its center. 如果将图像视图的中心限制在特定位置,则指南针图像应绕其中心旋转。

For example, you could control drag from the image view to the parent and choose Center in Container for both vertical and horizontal directions. 例如,您可以控制从图像视图到父视图的拖动,然后在垂直和水平方向上选择“在容器中居中”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM