1

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


在 .h 我以这种方式连接它

#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 +??);
    }];
}

??=我仍然必须决定我希望它移动多少这样,当我到达带有动画的屏幕时应用程序就会崩溃

4

2 回答 2

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];
}

笔记

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

如果它仍然没有移动,请确保未选择“useAutolayout”检查:

在此处输入图像描述

仅仅因为我已经构建了它,请尝试通过点击来制作动画:

在您看来DidLoad:

- (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
于 2014-03-18T21:09:21.853 回答
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
    }];

这是我的动画代码

于 2014-03-18T21:10:51.200 回答