从NSMutableArray滚动图像

时间:2012-09-11 15:16:53

标签: iphone ios xcode animation uiimageview

我在我的博客应用程序中解析XML。当我解析它时,我创建了一个名为entry的对象,其中包含Title,Article,Link,Content和Image的属性。然后我有一个名为条目的可变数组,在解析完成后,我向条目添加对象条目。这样,为了获得tableview中某篇文章的标题,我可以调用

 RSSEntry *entry = [_Entries objectAtIndex:indexPath.row];
 NSString *title = entry.Title;

我想要做的是在tableview上面有一个UIImageView,并设置它滚动数组中的所有不同的UIImages,但不确定如何实现这一点。我知道animationImages属性,但不知道在所有这些中设置为NSArray,因为我使用具有属性的Mutable Array。任何建议都会有很大帮助。

3 个答案:

答案 0 :(得分:0)

我希望你知道如何将UIImageView放在tableview上面的xob / storyboard中。 比我不确定你想要对图像阵列做什么,但是如果它能满足你的需求而不是刷卡识别器,并刷图像

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    UISwipeGestureRecognizer* recognizerLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)];

    recognizerLeft.direction =UISwipeGestureRecognizerDirectionLeft;
    recognizerLeft.numberOfTouchesRequired =1;

    [self.view addGestureRecognizer:recognizerLeft];
    [recognizerLeft release];

    UISwipeGestureRecognizer* recognizerRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)];

    recognizerRight.direction =UISwipeGestureRecognizerDirectionRight;
    recognizerRight.numberOfTouchesRequired =1;

    [self.view addGestureRecognizer:recognizerRight];    
    [recognizerRight release];

}

- (IBAction)swipeLeft{
    //NSLog(@"Swipe left");
    if(pager.currentPage < pager.numberOfPages - 1){
        pager.currentPage = (pager.currentPage + 1 );
        [self pagerValueChanged];
    }

}

- (IBAction)pagerValueChanged 
{
    NSData* imgData = [images objectAtIndex:pager.currentPage];
    UIImage* img = [[UIImage alloc]initWithData:imgData];
    imageView.image = img;

    //NSLog(@"img width: %f, img height: %f", img.size.width, img.size.height);

    [img release];
}

我的代码中也有分页控件和其他控件,但你可能不需要那些

答案 1 :(得分:0)

有两种选择:

a)通过迭代_Entries来收集单独数组中的所有图像,以设置animationImages数组值;

b)准备自己的视图,使用[NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:]在构造函数中初始化计时器,在selector更改显示的图像。要制作流畅的动画,您可以使用两个imageView子视图设置动画显示的imageView alpha从1到0,下一个imageView alpha从0到1.作为数据源,您可以使用{{1}的数组使用已知的密钥路径从那里获取图像。

第一个选项相当容易。

答案 2 :(得分:0)

使用数组枚举非常简单。以下代码将采用您已配置的滚动视图,并为其中的所有对象添加图像视图。它还将根据数组的计数动态调整滚动视图的内容大小。

[myMutableArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
    UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myMutableArray objectAtIndex:idx]]];
    [myImageView setFrame:CGRectMake(myScrollView.frame.size.width * idx, myScrollView.frame.origin.y, myScrollView.frame.size.width, myScrollView.frame.size.height)];
    [myScrollView addSubview:myImageView];
    [myScrollView setContentSize:CGSizeMake((myScrollView.frame.size.width * idx) + myScrollView.frame.size.width, myScrollView.frame.size.height)];
}];