0

我已经让我的 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];
4

1 回答 1

1

看起来您忘记创建数组了。在适当的时候这样的事情会做(假设 ARC):

imageArray = [NSMutableArray array];

很高兴这成功了。

于 2013-01-29T17:43:25.987 回答