我在iPhone应用程序中显示两个图像,一个在顶部,一个在底部。它们覆盖整个屏幕。用户通过滑动手势可以更改任一图像,具体取决于滑动开始的位置。
我希望图像随着动画过渡而改变。它目前无需动画,或整个屏幕转换。是否有可能在屏幕的一部分上进行转换?
我第一次加载图像(在viewDidLoad中):
// top image
UIImage *topImage = [UIImage imageNamed:@"top1.png"];
CGRect topframe = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
UIImageView *topView = [[UIImageView alloc] initWithFrame:topframe];
topView.image = topImage;
[self.view addSubview:topView];
[topView release];
// bottom image
UIImage *bottomImage = [UIImage imageNamed:@"bottom1.png"];
CGRect bottomframe = CGRectMake(0.0f, 240.0f, 320.0f, 240.0f);
UIImageView *bottomView = [[UIImageView alloc] initWithFrame:bottomframe];
bottomView.image = bottomImage;
[self.view addSubview:bottomView];
[bottomView release];
当我检测到滑动并检测到要更改两个图像中的哪一个时,我会调用这样的例程:
- (void)changeTopImage:(NSString *)newImage {
UIImage *topImage = [UIImage imageNamed:newImage];
CGRect topframe = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
UIImageView *topView = [[UIImageView alloc] initWithFrame:topframe];
topView.image = topImage;
[self.view addSubview:topView];
[topView release];
}
基本上,我不断地将图像加载到彼此之上。这是最好的方法吗,特别是在内存管理方面?
我尝试过的所有其他内容,使用下面的技术,使整个屏幕过渡:
[UIView beginAnimations:nil context:nil];
...
[UIView commitAnimations];
感谢任何关于我应该采用哪种方式的线索。
答案 0 :(得分:0)
好吧,一个简单的方法可能是这个(重复底部图像):
第一次在方法中加载时,不发布topView , 但是在.h文件中声明它, 并使用tempView:
@interface YourClass: UIViewController{
UIImageView *topView;
UIImageView *tempView;
}
通过这种方式,您可以调用它们来移动它们并在加载新图像时将其删除
然后在.m:
编辑:一些更正(见coco的评论):
[a] [b]第4行= [c]第11行:
- (void)changeTopImage:(NSString *)newImage {
UIImage *topImage = [UIImage imageNamed:newImage];
//CGRect topframe = CGRectMake(0.0f, 0.0f, (320.0f + 320), 240.0f);
CGRect topframe = CGRectMake((0.0f + 320), 0.0f, 320.0f, 240.0f);
tempView = [[UIImageView alloc] initWithFrame:topframe];
//topView.image = topImage;
tempView.image = topImage;
[self.view addSubview:tempView];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView beginAnimations:@"animation" context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
tempView.center = topView.center;
// do this to push old topView away, comment next line if wanna new image just cover it
//topView.center = CGPointMake(topView.x - 320, topView.y);
topView.center = CGPointMake(topView.center.x - 320, topView.center.y);
// call a method when animation has finished:
[UIView setAnimationDidStopSelector:@selector(endOfAnimation:finished:context:)];
[UIView commitAnimations];
}
- (void)endOfAnimation:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
[topView removeFromSuperview];
topView = tempView;
[tempView release];
tempView = nil;
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
- (void)dealloc {
if (tempView != nil) {
[tempView release];
}
[topView release];
[super dealloc];
}