简体   繁体   English

有关水平滚动视图IOS的建议

[英]Advice for horizontal scroll view IOS

I'm trying to create horizontal scroll view(s). 我正在尝试创建水平滚动视图。 Each view containing one image. 每个视图包含一个图像。 [done]

Initially when app is opened I'd display couple of images ie 3, so user can scroll back and forth between images. 最初,当打开应用程序时,我会显示几个图像,即3个,因此用户可以在图像之间来回滚动。 [done]

However I want to be able to go to another view controller and pick another image(s) maybe two for example and display five images in the scroll view instead of displaying 3 initially. 但是我希望能够转到另一个视图控制器并选择另一个图像(例如两个),并在滚动视图中显示五个图像,而不是最初显示三个图像。

How would one do that? 一个人怎么做? to "re-refresh" the initial scroll view ? 要“刷新”初始滚动视图?

Update Should I use delegate for this communication between view controllers? 更新我应该使用委托在视图控制器之间进行这种通信吗? or how is this done? 或如何完成? 1 main controller, other containing image selection? 1个主控制器,其他包含图像选择?

This part above and much more explained by article here .(not advertising, I hope it will help someone as well). 上面的这一部分以及此处的文章对此进行了更多的解释(不是广告,我希望它也会对某人有所帮助)。

Bounty update part 1 : 赏金更新第1部分

I think now that I found my way around delegates, I have additional question that I cannot find answer to, all examples I saw were about updating table views. 我认为现在我可以绕过代表了,我还有一个我找不到答案的问题,我看到的所有示例都是关于更新表视图的。

Bounty part 2 : If I were to have a scrollview inside my view controller and some nsimages inside scroll view. 赏金第2部分 :如果我要在View Controller中拥有一个scrollview,并在滚动视图中拥有一些nsimages。 ie : 即:

 - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *images = ...some images array; for (int i = 0; i < images.count; i++) { CGRect frame; frame.origin.x = self.scrollView.frame.size.width * i; frame.origin.y = 0; frame.size = self.scrollView.frame.size; UIImageView *subview = [[UIImageView alloc] initWithFrame:frame]; subview.image = [images objectAtIndex:i]; [self.scrollView addSubview:subview]; } self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height); } 

And let say my view controller implemented some delegate method didAddImage or didRemoveImage . 假设我的视图控制器实现了某些委托方法didAddImagedidRemoveImage

Meaning that about images array would get updated. 这意味着关于images数组将得到更新。

Actual bounty question : 实际赏金问题:

How would one actually "tell" the view controller, OK now your scrollview has one more image to display, please re-fresh or reload? 如何实际地“告诉”视图控制器,现在您的滚动视图又要显示一幅图像,请重新刷新或重新加载?

If I were to have a table view instead scroll view and was inserting images I'd do it something like this(in my delegate method) : 如果我要有一个表格视图而不是滚动视图并插入图像,我会这样做(在我的委托方法中):

 -(void) mockDelegatetablemethod:(...) ...{ [self.images addObject:image]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.images count] - 1 inSection:0]; [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; [self dismissViewControllerAnimated:YES completion:nil]; } 

How would one do this to the scrollview? 如何对滚动视图执行此操作?

Bounty update part 3: 赏金更新第3部分:

This is "simple" case described above, naturally I'd have to support removing images also as well as if one wants to remove all images and add some new. 这是上面描述的“简单”情况,自然,我也必须支持删除图像,以及是否要删除所有图像并添加一些新图像。

How would one actually "tell" the view controller, OK now your scrollview has one more image to display, please re-fresh or reload? 如何实际地“告诉”视图控制器,现在您的滚动视图又要显示一幅图像,请重新刷新或重新加载?

Well, you would just add the new image as a subview of the scroll view. 好吧,您只需将新图像添加为滚动视图的子视图即可。 So something like: 所以像这样:

- (void)addImage:(UIImage*)image {
    [images addObject:image];

    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * images.count;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
    subview.image = image;
    [self.scrollView addSubview:subview];

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
}

Or you could have a method that goes and "resets" the scroll view by removing all subviews and then re-adds all the images in your images array. 或者,您可以通过删除所有子视图,然后重新添加图像数组中的所有图像,来“重置”滚动视图。 Then call this method in viewDidLoad instead of your code there so that you're not duplicating code. 然后在viewDidLoad而不是在那里的代码中调用此方法,以免重复代码。

Basically, there's no silver bullet here. 基本上,这里没有银弹。 You roll your own. 你自己滚。

You're going to need make your image scroller a class that you can tell to load up certain images. 您将需要使图像滚动器成为一个可以告诉您加载某些图像的类。 You'll need a reference to the existing image scroller, pass that pointer into your other "pick more images" controller. 您将需要对现有图像滚动器的引用,并将该指针传递到另一个“拾取更多图像”控制器中。 Then the "pick more images" controller can tell the image scroller that there is a new list of images. 然后,“拾取更多图像”控制器可以告诉图像滚动器有新的图像列表。

This is already implemented by Apple in their iOS sample project "PhotoScroller". 苹果已经在其iOS示例项目“ PhotoScroller”中实现了此功能。 It has view reuse as well. 它还具有视图重用。 They even have a WWDC 2010 video on this. 他们甚至对此都有WWDC 2010视频。 I think its advanced scrollview techniques OR designing apps with scrollviews. 我认为其先进的scrollview技术或使用scrollviews设计应用程序。 Shouldn't be hard to port over to mac 应该不难移植到Mac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM