rotate an imageView While moving it

2019-08-03 16:28发布

So I have an UIView called image, and i am trying to get it to rotate while i am moving the view up and down,here is what I've done so far,but it makes the picture rotate and not go down:

{
    return centerY - height / 2 <= 10 ||
    centerY + height/ 2 >= self.view.bounds.size.height - 10;
}

-(void)moveUpDownTimerCallback
{
    [UIView commitAnimations];
    verticalSpeed += acceleration;
    degrees+=degreechange;
    CGSize sz = image.bounds.size;
    image.transform = CGAffineTransformMakeRotation(degrees * M_PI/180),image.layer.anchorPoint =  
    CGPointMake(rotPointx/sz.width,rotPointy/sz.height);rotPointy = image.center.y;      
   rotPointx = image.center.x;

    CGPoint newCenter = CGPointMake(image.center.x, image.center.y + verticalSpeed);
    if ([self hasCollided: image.center.y + verticalSpeed imageHeight:           
    image.bounds.size.height]) {     
        acceleration = 0;
        [self moveUpDown: 0];

    }
    else {
        image.center = newCenter;
    }
}

-(void)moveUpDown:(int) vSpeed
{
    verticalSpeed = vSpeed;
    if (verticalSpeed != 0 || acceleration != 0) {
       if (timer == nil) 
          timer =[NSTimer scheduledTimerWithTimeInterval:0.05
       target:self
       selector:@selector(moveUpDownTimerCallback)
       userInfo:nil
       repeats:YES];
       }
    }
    else {
        if (timer != nil) {
            [timer invalidate];
            timer = nil;
        }
    }
}

any help would be much appreciated (:

2条回答
不美不萌又怎样
2楼-- · 2019-08-03 17:07

You are taking completely the wrong approach. You should use the new block-based UIView animation methods like animateWithDuration:animations: and it's cousins.

You can animate rotation and movement at the same time.

If you can limit your development to iOS 7 there are new keyframe-based animation methods that let you do a timed sequence of animations quite easily.

查看更多
够拽才男人
3楼-- · 2019-08-03 17:10

try rotating imageview with this method

        CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        animation.fromValue = @0.0f;
        animation.toValue = @(2*M_PI);
        animation.duration = 1.0f;             // this might be too fast
        animation.repeatCount = HUGE_VALF;     // HUGE_VALF is defined in math.h so import it
        [self.imageview1.layer addAnimation:animation forKey:@"rotation"];

and moving view with this method

CGRect frameBottomview1 = self.yourview.frame;
frameBottomview1.origin.y = 300;
   [UIView animateWithDuration:0.7 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    [self.yourview setFrame:frameBottomview1 ];

                } completion:nil];

enter image description here

查看更多
登录 后发表回答