Rotating Two UIImageView in X Direction

送分小仙女□ 提交于 2020-01-14 03:15:08

问题


i wanna rotate two images i put one images behind the other but when i rotate them with the following code the back image will be above the front image and i assign their ZPOstion but their is no change

this link for my view before rotate http://i39.tinypic.com/ivv2ah.png

and after rotate http://i43.tinypic.com/ic05xj.png

how this happen

 [UIView animateWithDuration:0.2 animations:
     ^{
         CALayer *myLayer=self.image2.layer;
         CABasicAnimation *animation = [CABasicAnimation  animationWithKeyPath:@"transform.rotation.x"];
         animation.fromValue = [NSNumber numberWithFloat:(1 * M_PI/Down)]; 
         animation.toValue = [NSNumber numberWithFloat:(1 * M_PI/UP)]; 
         animation.repeatCount =0; 
         animation.duration=0.2;
         [animation setRemovedOnCompletion:NO];
         animation.timingFunction = [CAMediaTimingFunction
                                     functionWithName:kCAMediaTimingFunctionLinear]; 
         [self.image2.layer addAnimation:animation forKey:@"transform.rotation.x"];
         CATransform3D transform = CATransform3DIdentity;
         transform.m34 = (1/500.0f);
         transform = CATransform3DRotate(transform, (1 * M_PI/UP), 1, 0,0);
         myLayer.transform=transform;
     }
   completion:^(BOOL finished) 
     {
         self.image2.postion=@"UP";
     }];


    [UIView animateWithDuration:0.2 animations:
     ^{
         CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
         animation.fromValue = [NSNumber numberWithFloat:(1 * M_PI/Down)]; 
         animation.toValue = [NSNumber numberWithFloat:(1 * M_PI/UP)]; 
         animation.repeatCount =0;
         animation.duration=0.2;
         [animation setRemovedOnCompletion:NO];
         animation.timingFunction = [CAMediaTimingFunction
                                     functionWithName:kCAMediaTimingFunctionLinear]; 
         [card.layer addAnimation:animation forKey:@"transform.rotation.x"];
         CATransform3D transform = CATransform3DIdentity;
         transform.m34 = (1/500.0f);
         transform = CATransform3DRotate(transform, (1 * M_PI/UP), 1, 0,0);
         card.layer.transform=transform;
     }
    completion:^(BOOL finished) 
     {
         card.postion=@"UP";
     }];

CArdImage is Custom UIImageView and this is what i Change

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) 
{
    self.layer.anchorPoint=CGPointMake(0.5, 1.0);
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = (1/500.0f);
    transform = CATransform3DRotate(transform, (1 * M_PI/2.5), 0.5, 0,0);
    self.layer.transform=transform;
    self.postion=@"UP";
  }
  return self;
 }

回答1:


A probable reason for this is that the animations are not synchronized with each other. Try by putting both the animation blocks together:

[UIView animateWithDuration:0.2 animations:
 ^{
     CALayer *myLayer=self.image2.layer;
     CABasicAnimation *animation = [CABasicAnimation  animationWithKeyPath:@"transform.rotation.x"];
     animation.fromValue = [NSNumber numberWithFloat:(1 * M_PI/Down)]; 
     animation.toValue = [NSNumber numberWithFloat:(1 * M_PI/UP)]; 
     animation.repeatCount =0; 
     animation.duration=0.2;
     [animation setRemovedOnCompletion:NO];
     animation.timingFunction = [CAMediaTimingFunction
                                 functionWithName:kCAMediaTimingFunctionLinear]; 
     [self.image2.layer addAnimation:animation forKey:@"transform.rotation.x"];
     CATransform3D transform = CATransform3DIdentity;
     transform.m34 = (1/500.0f);
     transform = CATransform3DRotate(transform, (1 * M_PI/UP), 1, 0,0);
     myLayer.transform=transform;

     // second animation code
     CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
     animation2.fromValue = [NSNumber numberWithFloat:(1 * M_PI/Down)]; 
     animation2.toValue = [NSNumber numberWithFloat:(1 * M_PI/UP)]; 
     animation2.repeatCount =0;
     animation2.duration=0.2;
     [animation2 setRemovedOnCompletion:NO];
     animation2.timingFunction = [CAMediaTimingFunction
                                 functionWithName:kCAMediaTimingFunctionLinear]; 
     [card.layer addAnimation:animation2 forKey:@"transform.rotation.x"];
     CATransform3D transform = CATransform3DIdentity;
     transform.m34 = (1/500.0f);
     transform = CATransform3DRotate(transform, (1 * M_PI/UP), 1, 0,0);
     card.layer.transform=transform;
 }
completion:^(BOOL finished)
 {
     self.image2.postion=@"UP";
     card.postion=@"UP";
 }];


来源:https://stackoverflow.com/questions/10063290/rotating-two-uiimageview-in-x-direction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!