2014-04-17 24 views
5

Próbuję wykonać 360 obrót widoku, ale pod kątem 45 stopni.Animacja iOS Animacja obrotu pod kątem

Ci się to

enter image description here

nie mogę dowiedzieć się, aby to zrobić.

Do tej pory udało mi się to

CABasicAnimation* animation = [CABasicAnimation 
           animationWithKeyPath:@"transform.rotation.y"]; 
animation.fromValue = @(0); 
animation.toValue = @(2 * M_PI); 
animation.duration = 1; 

[self.layer addAnimation:animation forKey:@"rotation"]; 

Które obraca go na jego osi y, ale to, co chcę jest dla tej osi Y, aby być nachylona 45 stopni przed jego obrócił.

Odpowiedz

1

Po pierwsze, powinieneś zobaczyć poniższy link, który wyjaśnia, że ​​różnice między "ramkami" i "ograniczeniami". UIView frame, bounds and center

A teraz, oto moja odpowiedź.

/* add the 4 lines below */ 
self.transform = CGAffineTransformMakeRotation(M_PI_4); 
self.layer.bounds = CGRectMake(0.0f, 0.0f, 60.0f, 200.0f); 
self.layer.position = CGPointMake(150.0f, 300.0f); 

CABasicAnimation* animation = [CABasicAnimation 
           animationWithKeyPath:@"transform.rotation.y"]; 
animation.fromValue = @(0); 
animation.toValue = @(2 * M_PI); 
animation.duration = 1; 

[self.layer addAnimation:animation forKey:@"rotation"]; 
0

1- zmienić punkt zakotwiczenia do górnej prawej krawędzi

self.someView.layer.anchorPoint = CGPointMake(1.0, 0.5); 

2- obracać widok, 45 lub 35 stopni

CGFloat radians = (M_PI/180) * (-35); 
self.someView.transform = CGAffineTransformRotate(self.someView.transform, radians); 

3- obracanie widoku od osi X

CGFloat radians = (M_PI/180) * (180); 
[UIView animateWithDuration:1 animations:^{ 
     self.someView.layer.transform = CATransform3DRotate(self.someView.layer.transform,radians , 1, 0.0, 0.0); 

    } completion:^(BOOL finished) { 
     if(finished) { 
     } 
    }]; 

działa jak czar :)

0

spróbuj tego;

CABasicAnimation* animation = [CABasicAnimation 
          animationWithKeyPath:@"transform"]; 

CATransform3D rtfm = CATransform3DMakeRotation(2 * M_PI , 1.0f, 1.0f, 0.0f); 
rtfm.m34 = -0.0015f; 

animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
animation.toValue = [NSValue valueWithCATransform3D:rtfm]; 
animation.duration = 1; 

[self.layer addAnimation:animation forKey:@"rotation"]; 
0

Oto przykład Xamarin iOS użyć do klapy narożnik przycisku kwadratowy, jak ucha psa (łatwo przenosić do obj-c):

enter image description here

Metoda 1: użyciu animacji rotacji w 1 zarówno x i y osi (przykłady w Xamarin.iOS, ale łatwo przenośny obj-c):

AnimateNotify(0.10, 0, UIViewAnimationOptions.CurveEaseOut | UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.BeginFromCurrentState,() => 
{ 
    // note the final 3 params indicate "rotate around x&y axes, but not z" 
    var transf = CATransform3D.MakeRotation(-1 * (nfloat)Math.PI/4, 1, 1, 0); 
    transf.m34 = 1.0f/-500; 
    Layer.Transform = transf; 
}, null); 

Metoda 2: tylko dodać rotację x-axis i y-axis obrót do CAAnimationGroup więc uruchomić w tym samym czasie:

AnimateNotify(1.0, 0, UIViewAnimationOptions.CurveEaseOut | UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.BeginFromCurrentState,() => 
{ 
    nfloat angleTo = -1 * (nfloat)Math.PI/4; 
    nfloat angleFrom = 0.0f ; 
    string animKey = "rotate"; 

    // y-axis rotation 
    var anim = new CABasicAnimation(); 
    anim.KeyPath = "transform.rotation.y"; 
    anim.AutoReverses = false; 
    anim.Duration = 0.1f; 
    anim.From = new NSNumber(angleFrom); 
    anim.To = new NSNumber(angleTo); 

    // x-axis rotation 
    var animX = new CABasicAnimation(); 
    animX.KeyPath = "transform.rotation.x"; 
    animX.AutoReverses = false; 
    animX.Duration = 0.1f; 
    animX.From = new NSNumber(angleFrom); 
    animX.To = new NSNumber(angleTo); 

    // add both rotations to a group, to run simultaneously 
    var animGroup = new CAAnimationGroup(); 
    animGroup.Duration = 0.1f; 
    animGroup.AutoReverses = false; 
    animGroup.Animations = new CAAnimation[] {anim, animX}; 
    Layer.AddAnimation(animGroup, animKey); 

    // add perspective 
    var transf = CATransform3D.Identity; 
    transf.m34 = 1.0f/500; 
    Layer.Transform = transf; 

}, null);