2015-01-13 4 views
0

나는 일련의 UIImageViews을 모두 IBOutlet 컬렉션에 엮어서 설정된 가속 속도로 어떤 각도로든 움직이고 싶습니다.가속도가있는 각도로 UIImage 이동하기

제가하는 첫 번째 일은 무작위로 (즉, 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이

감사를 해결하는 데 도움이 될 것입니다 생각하지 않습니다

+0

'UIImage'자체는 위치 나 각도가 없습니다. 보기 만 화면에 상대적으로 서로 위치합니다. 'CAAnimation' 서브 클래스를 사용하여 뷰의 속성을 애니메이트 할 수 있으며'UIImage'를 그릴 UIImageView가 있습니다. 2-4 픽셀은 가속이 아니며 기껏해야 거리라는 점에 유의하십시오. –

+0

좋아요, CAANimation을 조사해야합니다 –

+0

아크 탄젠트 함수를 살펴보십시오. 각도를 계산하는 데 사용되며 올바른 'x'및 'y'위치를 결정하는 데 도움이됩니다. – picciano

답변

0

저는 현재 제가 사용하고있는/worker에 대한 대안적인 대안이 있습니다.

// 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"); 


                 }];        
            }]; 



      }); 

이것은 문제의 해결책입니다. 많은 감사합니다.