如何从顶部到底部垂直移动UIImage

时间:2010-12-14 17:00:43

标签: iphone iphone-sdk-3.0 ios4

我希望UIImage在屏幕上垂直向下移动,当它到达终点时,它应该再次从顶部开始。我该怎么办?

请帮忙 感谢

2 个答案:

答案 0 :(得分:1)

假设您的UIImage位于UIImageView中,图像视图的大小为320 x 480 ......

动画块可以在完成后调用方法。此方法重置UIImageView的位置,然后启动动画。

动画块:

-(void)animate {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.3];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(myCallback:finished:context:)];

    CGRect frame = yourImageView.frame;
    frame.origin = CGPointMake(0, 480);
    yourImageView.frame = frame;

    [UIView commitAnimations];

}

回调:

(void)myCallback:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

    CGRect frame = yourImageView.frame;
    frame.origin = CGPointMake(0, 0)
    yourImageView.frame = frame;
    [self animate];
}

答案 1 :(得分:0)

这就是我编写代码的方式 moving.h

#import <UIKit/UIKit.h>
@interface movingViewController : UIViewController{ 
UIImageView *imageView1;
}
@property (retain , nonatomic) UIImageView *imageView1;
@end

moving.m

#import "movingViewController.h"
@synthesize imageView1;
-(void) animate {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(myCallback:finished:context:)];

CGRect frame = imageView1.frame;
frame.origin = CGPointMake(0, 480)
imageView1.frame = frame;

[UIView commitAnimations];  
}

- (void)viewDidLoad {

    [super viewDidLoad];
 }


 -(void)myCallback:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

CGRect frame = imageView1.frame;
frame.origin = CGPointMake(0, 0)
imageView1.frame = frame;
[self animate];
}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];}
- (void)viewDidUnload {}
- (void)dealloc {
    [imageView1 dealloc];
    [imageView1 release];
    [super dealloc];
}
@end

我很抱歉提出简单的问题,但我对Xcode来说是非常新的

是正确的方法