将UIImageView从A移动到B.

时间:2014-03-18 21:06:33

标签: ios objective-c animation uiimageview animatewithduration

我需要将UIImageView从x,y移动到x,y1。 这就是我所拥有的。我在-(void)animation中尝试了很多不同的代码,大多数使用animateWithDuration,但似乎都没有。它只是没有做任何事情。我肯定错过了什么。 .h是否正确?你会如何移动图像?然后我必须循环,我怎么能这样做?这是我第一次使用动画,我无法弄清楚要做什么,它必须简单。在此先感谢。


in。我用这种方式连接

#import <UIKit/UIKit.h>

@interface ImageBeaconViewController : UIViewController {

    __weak IBOutlet UIImageView *blueView;
}

@end

in .m我有

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

....

 - (void)animation{
    [UIView animateWithDuration:3 animations:^{
        blueView.alpha = 1;
        blueView.center = CGPointMake(blueView.center.x , blueView.center.y +??);
    }];
}

?? =我仍然需要决定我想要它移动多少 这样,一旦我使用动画

进入屏幕,应用程序就会崩溃

2 个答案:

答案 0 :(得分:1)

像这样添加方法:

- (void) animateToPoint:(CGPoint)point {
    [UIView animateWithDuration:3 animations:^{
        blueView.alpha = 1;
        blueView.center = point;
    }];
}

这样打电话:

- (void) viewDidAppear:(BOOL)animated {
    int distanceToSlide = 40; // will slide down 40px (use -40 to go up)
    CGPoint targetPoint = CGPointMake(blueView.center.x, blueView.center.y + distanceToSlide);
    [self animateToPoint:targetPoint];
}

注意

请注意,如果您在viewDidAppear中设置了动画,我将动画调用放在viewDidLoad,因为您的视图尚未显示,因此您看不到任何内容。

如果它还没有移动,请确保&#34; useAutolayout&#34;没有选中检查:

enter image description here

仅仅因为我已经构建了它,尝试使用它来点击动画:

在viewDidLoad中:

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
    tap.numberOfTapsRequired = 1;
    [tap addTarget:self action:@selector(handleTap:)];
    [self.view addGestureRecognizer:tap];
}

然后,使用这些:

- (void) handleTap:(UITapGestureRecognizer *)tap {
    int distanceToSlide = 40; // will slide down 40px (use -40 to go up)
    CGPoint targetPoint = CGPointMake(blueView.center.x, blueView.center.y + distanceToSlide);
    [self animateToPoint:targetPoint];
}

- (void) animateToPoint:(CGPoint)point {
    [UIView animateWithDuration:3 animations:^{
        blueView.alpha = 1;
        blueView.center = point;
    }];
}

这是一种循环方式:

@implementation ImageBeaconViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    pointA = CGPointMake(40, 40);
    pointB = CGPointMake(250, 350);

    blueView.center = pointA;
}

- (void) viewDidAppear:(BOOL)animated {
    [self animate];
}

- (void) animate {
    if (CGPointEqualToPoint(_blueView.center, pointA)) {
        [self animateToPoint:pointB];
    }
    else {
        [self animateToPoint:pointA];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) animateToPoint:(CGPoint)point {
    [UIView animateWithDuration:1.0 animations:^{
        _blueView.alpha = 1;
        _blueView.center = point;
    } completion:^(BOOL finished) {
        if (finished) {
            [self animate];
        }
    }];
}

@end

答案 1 :(得分:0)

[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        blueView.alpha = 1;
        blueView.center = CGPointMake(blueView.center.x , blueView.center.y + 100);
    } completion:^(BOOL finished) {

       //if you need any thing after animation is done
    }];

这是他们的动画代码