2012-11-28 38 views
6

我正在制作一个iPhone应用程序,我需要在UIScrollView中显示一些图像,左右两侧将出现两个按钮,用户可以点击按钮,它会显示下一个或上一张图片。在底部还有一个按钮,当用户点击该按钮时,它会显示我们在scrollview中选择的图像。我想知道我们如何在该滚动视图内显示多个图像,并选择底部按钮时,如何找到哪个图像在滚动视图中。如何在iOS中的UIScrollView中显示多个图像

+0

你做了什么至今 –

+0

似乎得到了直接的代码?已经做了并且努力了吗? –

回答

5

从danielbeard.wordpress一个例子.COM

如果你的形象的名字不仅仅是数字:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    [scrollView setPagingEnabled:YES]; 
    [scrollView setAlwaysBounceVertical:NO]; 

    NSArray *imagesArray = [NSArray arrayWithObjects:@"img1.png", @"img2.png", @"img3.png", nil]; 

    for (int i = 0; i < [imagesArray count]; i++) 
    { 
     CGFloat xOrigin = i * scrollView.frame.size.width; 

     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)]; 
     [imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]]; 
     [imageView setContentMode:UIViewContentModeScaleAspectFit]; 

     [scrollView addSubview:imageView]; 
    } 

    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [imagesArray count], scrollView.frame.size.height)]; 
+0

我做到了这一点,并适用于肖像模式。但如果我有不同的图像阵列,我该如何做它的横向模式。请给我一些建议。 – Mak13

+0

当屏幕旋转到横向模式时,您应该检测。一旦检测到更改图像数组中的图像名称,然后删除旧的滚动视图,并用新的图像数组绘制一个新的。 – tnylee

+0

我只做了同样的日子。无论如何感谢回复。 – Mak13

2

Apple.developerPhotoScroller演示了如何使用嵌入式UIScrollViews和CATiledLayer来创建用于显示和分页可以单独摇镜和变焦照片丰富的用户体验。

CATiledLayer用于增加使用高分辨率图像或大量照片进行分页,平移和缩放的性能。

0

你只是单纯的第一 创建的运行时间和图像的新观点,并指定标签上标记值像1000或者100和增量各方意见,然后该视图添加到滚动视图和左,右移动两个按钮,使他们的出口行为,并添加所有的滚动视图的子视图的可变数组这样

 indexStart=100; 
UIView *AddView=[[UIView alloc]initWithFrame:CGRectMake(55, 0, 100, 100)]; 

       AddView.tag=indexStart; 
       btnTap.tag=indexStart; 

       UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; 



       [AddView addSubview:imgview]; 

       imgview.image=image; 
       imgview.tag=1000; 
       [ScrollView addSubview:AddView]; 
       [imgArray addObject:AddView]; 
你用循环来做到这一点,你用这个左右键

代码,在这个代码中,editImgeView是图像视图nbetween左右键,你显示出图像,你只是简单的indexstart ++或 - 如果用户选择左边或右边按钮

for (int index = 0; index < [imgAddArrayAfter count]; index ++) { 
     NSLog(@"index %d",index); 
     UIView *vc=[imgArray objectAtIndex:index]; 
     NSLog(@"view tag %d",vc.tag); 
     if(vc.tag == indexStart) 
     { 

      UIImageView *modalView=(UIImageView *) [vc viewWithTag:1000]; 
      editImgeView.image=modalView.image; 
     } 
    } 

我希望你明白;-)

0
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    scrollView.contentSize = CGSizeMake(320, 465); 

    [scrollView setScrollEnabled:YES]; 
    [scrollView setPagingEnabled:YES]; 
    [scrollView setAlwaysBounceVertical:NO]; 
    [self.view addSubview:scrollView]; 

    NSMutableArray *arrImage = [NSMutableArray arrayWithObjects:@"food1.jpeg", @"food2.jpeg", @"food3.jpeg",@"food4.jpeg", @"food5.jpeg", @"food6.jpeg",@"food7.jpeg", @"food8.jpeg", @"foo9.jpeg",@"food10.jpeg", @"food11.jpeg", @"food12.jpeg",@"food13.jpeg", @"food14.jpeg", @"food15.jpeg", @"food16.jpeg", nil]; 

    for (int i = 0; i < [arrImage count]; i++) 
    { 
     CGFloat xOrigin = i * scrollView.frame.size.width; 

     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)]; 
     [imageView setImage:[UIImage imageNamed:[arrImage objectAtIndex:i]]]; 
     [imageView setContentMode:UIViewContentModeScaleAspectFit]; 

     [scrollView addSubview:imageView]; 
    } 

    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [arrImage count], scrollView.frame.size.height)]; 
} 
相关问题