【发布时间】:2013-01-13 05:25:42
【问题描述】:
我已启用我的 Cocoa Touch 应用程序,可以通过向左或向右滑动来更改历史位置来进行导航。动画有点像 Android 的“卡片”风格。向左滑动 (
这很好用,但是当我想以其他方式滑动 (-->) 时,要返回,我需要获取上一个图像,并将其移动到当前视图上。现在,如果我只存储以前的图像,我就有了这个工作,但是如果我去
所以,解决方案很明显,使用 NSMutableArray 并将最新的图像扔到数组的前面,当你向另一个方向滑动时,只需使用数组中的第一个图像。但是,当我开始动画时,图像永远不会显示。它什么也没显示。以下是您应该看到的所需方法:
-(void)animateBack {
CGRect screenRect = [self.view bounds];
UIImage *screen = [self captureScreen];
[imageArray insertObject:screen atIndex:0]; //Insert the screenshot at the first index
imgView = [[UIImageView alloc] initWithFrame:screenRect];
imgView.image = screen;
[imgView setHidden:NO];
NSLog(@"Center of imgView is x = %f, y = %f", imgView.center.x, imgView.center.y);
CGFloat startPointX = imgView.center.x;
CGFloat width = screenRect.size.width;
NSLog(@"Width = %f", width);
imgView.center = CGPointMake(imgView.center.x, imgView.center.y);
[self.view addSubview: imgView];
[self navigateBackInHistory];
[UIView animateWithDuration:.3 animations:^{
isSwiping = 1;
imgView.center = CGPointMake(startPointX - width, imgView.center.y);
} completion:^(BOOL finished){
// Your animation is finished
[self clearImage];
isSwiping = 0;
}];
}
-(void)animateForward {
CGRect screenRect = [self.view bounds];
//UIImage *screen = [self captureScreen];
UIImage *screen = [imageArray objectAtIndex:0]; //Get the latest image
imgView = [[UIImageView alloc] initWithFrame:screenRect];
imgView.image = screen;
[imgView setHidden:NO];
NSLog(@"Center of imgView is x = %f, y = %f", imgView.center.x, imgView.center.y);
CGFloat startPointX = imgView.center.x;
CGFloat width = screenRect.size.width;
NSLog(@"Width = %f", width);
imgView.center = CGPointMake(imgView.center.x - width, imgView.center.y);
[self.view addSubview: imgView];
[UIView animateWithDuration:.3 animations:^{
isSwiping = 1;
imgView.center = CGPointMake(startPointX, imgView.center.y);
} completion:^(BOOL finished){
// Your animation is finished
[self navigateForwardInHistory];
[self clearImage];
isSwiping = 0;
}];
}
-(void)clearImage {
[imgView setHidden:YES];
imgView.image = nil;
}
-(void)navigateBackInHistory {
[self saveItems:self];
[self alterIndexBack];
item = [[[LEItemStore sharedStore] allItems] objectAtIndex:currentHistoryIndex];
[self loadItems:self];
}
-(void)navigateForwardInHistory {
[imageArray removeObjectAtIndex:0]; //Remove the latest image, since we just finished swiping this way.
[self saveItems:self];
[self alterIndexForward];
item = [[[LEItemStore sharedStore] allItems] objectAtIndex:currentHistoryIndex];
[self loadItems:self];
}
请注意,imgView 是一个类级别的 UIImageView,而 imageArray 是一个类级别的数组。
有什么想法吗?谢谢。
编辑
这是我的 .m 顶部的代码来初始化它。还是不行。
.....
NSMutableArray *imageArray;
- (void)viewDidLoad
{
[super viewDidLoad];
imageArray = [imageArray initWithObjects:nil];
【问题讨论】:
-
imageArray是否在某处创建和初始化?当您尝试添加或检索图像时,您是否检查过它不是 nil ? -
哦...等等,是的,我认为我没有初始化它。会去检查的。
-
@FirozeLafeer,见编辑。
-
你实际上并没有创建数组,试试
imageArray = [NSMutableArray array];(假设你使用的是ARC) -
@FirozeLafeer,呵呵,非常感谢。效果很好!将其发布为答案,我很乐意接受。 :)
标签: objective-c cocoa-touch uiimage nsmutablearray