简体   繁体   English

将UIImageView从A移到B

[英]Move UIImageView from A to B

I need to move an UIImageView from x,y to x,y1. 我需要将UIImageView从x,y移动到x,y1。 This is what i have. 这就是我所拥有的。 I tried lots of different codes in -(void)animation , most using animateWithDuration but none seems to work. 我在-(void)animation尝试了许多不同的代码,大多数使用了animateWithDuration但似乎没有任何效果。 It just doesn't do anything. 它什么也没做。 I must be missing something. 我肯定错过了什么。 Is .h correct? .h是正确的吗? How would you make the image move? 您将如何移动图像? I then have to loop that, how can i do that? 然后,我必须循环播放,我该怎么做? It's my first time with animate, I can't figure out what to do, it has to be simple. 这是我第一次使用动画,我不知道该怎么做,它必须很简单。 Thanks in Advance. 提前致谢。


in .h I connected it this way 在.h中我以这种方式连接

#import <UIKit/UIKit.h>

@interface ImageBeaconViewController : UIViewController {

    __weak IBOutlet UIImageView *blueView;
}

@end

the in .m I've got 在.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 +??);
    }];
}

??= I still have to decide how much I want it to move This way the app crashes as soon as I get to the screen with the animation ?? =我仍然必须决定要移动多少,这样,一旦我进入带有动画的屏幕,应用程序就会崩溃

Add Method Like This: 像这样添加方法:

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

Call like this: 像这样打电话:

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

NOTE 注意

Notice that I put your animation call in viewDidAppear if you animate in viewDidLoad you won't see anything because your view isn't displayed yet. 请注意,如果您在viewDidLoad设置动画,则我会将动画调用放入viewDidAppear ,因为您的视图尚未显示,所以您将看不到任何内容。

If it's still not moving, make sure "useAutolayout" is not selected check this: 如果仍然没有移动,请确保未选中“ useAutolayout”,请检查以下内容:

在此处输入图片说明

Just because I already built it, try this for tap to animate: 只是因为我已经构建了它,所以请尝试以下操作以进行动画处理:

In your viewDidLoad: 在您的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];
}

Then, use these: 然后,使用这些:

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

Here's a way to loop it: 这是循环的一种方法:

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

this is them code for animation i have 这是我的动画代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM