使用加速度以一定角度移动aN图像

时间:2015-01-13 15:47:01

标签: objective-c core-animation uibezierpath

我在UIImageViews集合中连接了一系列IBOutlet,我希望它们以任何角度以设定的加速度移动。

我做的第一件事是随机地将它们放在一个角度(即:35,90,70,270度),现在我想移动它们。

图像是平面的图标,我想将每个平面图标以某个角度(无论它们当前所面对的角度)和某个加速度(例如2-4像素等)移动。

唯一的问题是我不确定如何在给定加速度下以给定角度移动UIImage

有没有办法用Core Animation做到这一点?

非常感谢


更新

我用单独的对象重写了该方法来处理飞机本身并尝试使用UIBezierPath;

   // Angles for airplane icons
    VICAirplane *plane1 = [[VICAirplane alloc] initWithX:48 withY:104 withAngle:140 withSpeed:5];
    VICAirplane *plane2 = [[VICAirplane alloc] initWithX:50 withY:250 withAngle:60 withSpeed:7];
    VICAirplane *plane3 = [[VICAirplane alloc] initWithX:280 withY:75 withAngle:240 withSpeed:12];
    VICAirplane *plane4 = [[VICAirplane alloc] initWithX:230 withY:270 withAngle:120 withSpeed:3];
    VICAirplane *plane5 = [[VICAirplane alloc] initWithX:148 withY:225 withAngle:85 withSpeed:10];
    VICAirplane *plane6 = [[VICAirplane alloc] initWithX:175 withY:225 withAngle:85 withSpeed:8];

    self.airplanes = @[plane1,plane2,plane3,plane4,plane5,plane6];

    UIImage *sprite = [UIImage imageNamed:@"Plane Shadow"];
    CGFloat spriteWidth = sprite.size.width;
    CGFloat spriteHeight = sprite.size.height;

    for (VICAirplane *planeObj in self.airplanes) {
        CALayer *plane = [CALayer layer];
        plane.bounds = CGRectMake(0, 0, spriteWidth, spriteHeight);
            plane.position = CGPointMake(planeObj.x, planeObj.y);
            plane.contents = (id)(sprite.CGImage);
            CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(planeObj.angle);
            [plane setAffineTransform:rotateTransform];
            [self.view.layer addSublayer:plane];

            // Animate it
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:CGPointMake(100.0, 100.0)];

[path addLineToPoint:CGPointMake(200.0, 200.0)];
    [path addLineToPoint:CGPointMake(100.0, 300.0)];

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    anim.path = path.CGPath;
    anim.rotationMode = kCAAnimationRotateAuto;
    anim.repeatCount = HUGE_VALF;
    anim.duration = 8.0;
    anim.autoreverses = YES;
    [plane addAnimation:anim forKey:@"race"];
    }

我想做的是根据每个平面的角度生成UIBezierPath并以速度移动

但我认为moveToPoint不会帮助解决这个问题

由于

1 个答案:

答案 0 :(得分:0)

我有一个潜在的替代解决方案,我现在正在使用/工作;

// Angles for airplane icons
    _sprite = [UIImage imageNamed:@"Plane"];
    CGFloat spriteWidth = _sprite.size.width;
    CGFloat spriteHeight = _sprite.size.height;
    self.velocity=1.0;

    //Random x&y points
    CGFloat x;
    CGFloat y;

    //VICAirplane *plane1 = [[VICAirplane alloc] initWithX:48 withY:104 withAngle:0 withSpeed:10];

    x = (CGFloat) (arc4random() % (int) self.view.frame.size.width);
    y = (CGFloat) (arc4random() % (int) self.view.frame.size.height);
    UIImageView *theBug = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, spriteWidth, spriteHeight)];
    [theBug setBackgroundColor:[UIColor redColor]];
    [theBug setImage:_sprite];
    [self.view addSubview:theBug];


    double delayInSeconds = 1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){




                 //Calculate the angle to rotate
                 CGFloat newX = (CGFloat) (arc4random() % (int) self.view.frame.size.width);
                 CGFloat newY = (CGFloat) (arc4random() % (int) self.view.frame.size.height);
                 CGPoint rotateToLocation = CGPointMake(newX, newY);

                 theBug.layer.anchorPoint = CGPointMake(0.5, 0);
                 float angleToRotate = atan2f(theBug.transform.tx-newX, theBug.transform.ty - newY);

                 if (theBug.transform.tx < newX)
                     angleToRotate *= 1;


                 [UIView animateWithDuration:4
                                       delay:0
                                     options:UIViewAnimationOptionCurveLinear
                                  animations:^{
                                      theBug.transform = CGAffineTransformRotate(theBug.transform, angleToRotate);
                                  } completion:^(BOOL finished) {

                                      [UIView animateWithDuration:2
                                                            delay:0.0
                                                          options:UIViewAnimationOptionCurveLinear
                                                       animations:^{
                                                           theBug.center = rotateToLocation;
                                                       } completion:^(BOOL finished) {
                                                           NSLog(@"Finished");


                                                       }];                             
                                  }];



            });

这是我对此问题的解决方案。非常感谢。