自动在屏幕上移动imageView,如L形iOS

时间:2015-02-18 10:08:03

标签: ios

我正在尝试在我的iOS应用中移动像L形状的图像。 我已经尝试过这段代码,但它只是在一行中移动图片,我不想要。

- (void)startApp {
  UIImageView *imageToMove =
      [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"man.jpg"]];
  imageToMove.frame = CGRectMake(10, 10, 100, 100);
  [self.view addSubview:imageToMove];

  // Move the image
  [self moveImage:imageToMove
         duration:1.0
            curve:UIViewAnimationCurveLinear
                x:70.0
                y:70.0];
}

- (void)moveImage:(UIImageView *)image
         duration:(NSTimeInterval)duration
            curve:(int)curve
                x:(CGFloat)x
                y:(CGFloat)y {
  // Setup the animation
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:duration];
  [UIView setAnimationCurve:curve];
  [UIView setAnimationBeginsFromCurrentState:YES];

  // The transform matrix
  CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
  image.transform = transform;

  // Commit the changes
  [UIView commitAnimations];
}

4 个答案:

答案 0 :(得分:0)

我会推荐这样的东西:

- (void)animateLikeL
{
    [UIView animateWithDuration:1.5f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
        self.yourImage.transform = CGAffineTransformMakeTranslation(0.0f, 10.0f);
    } completion:^(BOOL finished){

        if(finished == NO)
        {
            return;
        }
        [UIView animateWithDuration:1.5f
                            delay:0.0f
                            options:UIViewAnimationOptionCurveLinear
                            animations:^{
                                self.yourImage.transform = CGAffineTransformMakeTranslation(10.0f, 10.0f);
                            } completion:^(BOOL finished){
                               if(finished == NO)
                               {
                                  return;
                               }
                                self.yourImage.transform = CGAffineTransformIdentity;
                                [self animateLikeL];
                            }];
    }];
}

另一种尝试是使用self.yourImage.center播放arround。增加center.y以向下移动图像,然后增加center.x以向左移动。希望这会有所帮助。

答案 1 :(得分:0)

我是这样做的:circle是界面构建器大小为70 * 70的UIImage视图,框架设置为(10,10),然后将此代码粘贴在:

- (void)viewDidAppear:(BOOL)animated {

  [UIView animateWithDuration:1
      delay:0
      options:0
      animations:^{

          self.circle.frame = CGRectMake(10, 249, 70, 70);

      }
      completion:^(BOOL finished) {

          [UIView animateWithDuration:1
              delay:0
              options:0
              animations:^{

                  self.circle.frame = CGRectMake(150, 249, 70, 70);

              }
              completion:^(BOOL finished) {

                  NSLog(@"Complete");

              }];

      }];
}

答案 2 :(得分:0)

我只是链接动画。

[UIView animateWithDuration:duration/2 delay:0 options:curve animations:^{
    CGAffineTransform transform = CGAffineTransformMakeTranslation(x,0);
    image.transform = transform;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:duration/2 delay:0 options:curve animations:^{
        CGAffineTransform transform = CGAffineTransformMakeTranslation(x,y);
        image.transform = transform;
    } completion:nil];
}];

答案 3 :(得分:0)

这就是我所做的......谢谢。

     - (void)viewDidLoad
      {
         [super viewDidLoad];
         [self startAppForFirstImg];
         [self startAppForSecondImg];
         }

         //First Image coordinates.
         - (void)startAppForFirstImg{
          UIImageView *imageToMove =
          [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"man.jpg"]];
          imageToMove.frame = CGRectMake(180,280,100,100);
          [self.view addSubview:imageToMove];

           // Move the image
          [self moveFirstImage:imageToMove duration:2.0
            curve:UIViewAnimationCurveLinear x:0.0 y:-200.0];
            }  

          -(void)moveFirstImage:(UIImageView *)image duration:        (NSTimeInterval)duration
           curve:(int)curve x:(CGFloat)x y:(CGFloat)y{
            [UIView animateWithDuration:duration/2 delay:0 options:curve animations:^{
            CGAffineTransform transform = CGAffineTransformMakeTranslation(0,y);
            image.transform = transform;

             }
             completion:^(BOOL finished) {
            [UIView animateWithDuration:duration/2 delay:0 options:curve animations:^{
            CGAffineTransform transform =  CGAffineTransformMakeTranslation(-70,-200);
           image.transform = transform;
           } completion:nil];
           }];
           }
          //Second Image coordinates.
          - (void)startAppForSecondImg{
            UIImageView *imageToMove =
        [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"man.jpg"]];
          imageToMove.frame = CGRectMake(20,20,100,100);
           [self.view addSubview:imageToMove];

         // Move the image
         [self moveSecondImage:imageToMove duration:2.0
              curve:UIViewAnimationCurveLinear x:0.0 y:290.0];
         }

         -(void)moveSecondImage:(UIImageView *)image duration:           (NSTimeInterval)duration
           curve:(int)curve x:(CGFloat)x y:(CGFloat)y{
          [UIView animateWithDuration:duration/2 delay:0 options:curve  animations:^{
           CGAffineTransform transform =   CGAffineTransformMakeTranslation(0,y);
           image.transform = transform;

           }
                 completion:^(BOOL finished) {
                 [UIView animateWithDuration:duration/2 delay:0 options:curve animations:^{
                    CGAffineTransform transform = CGAffineTransformMakeTranslation(90,290);
                       image.transform = transform;
                 } completion:nil];
                }];
                }