iOS - Horizontally sliding items

假装没事ソ 提交于 2019-12-21 03:01:15

问题


I have some picture, let's say like rectangular piece of paper. I want to position it on the center of the screen, and when user slides left or right with its finger (let's say left), that piece of paper should go left (with the finger), and from the right should come new piece of paper with some new information.

I hope that my description isn't confusing. I guess that this functionality should be easy to do with iOS SDK, I just don't know what to use ...

Thanks for any help ;)


回答1:


You'll want to use a UIScrollView for that. You add your "pieces of paper" -- which I assume will be views (UIImageViews perhaps?) -- as subviews, and set the contentSize property of the scroll view to fit them all. You might also want to set the showsHorizontalScrollIndicator property of the scroll view to NO, to hide the scroller.

Off the top of my head, I imagine something like this:

UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
aScrollView.showsHorizontalScrollIndicator = NO;

CGFloat paperWidth = 320;
NSUInteger numberOfPapers = 5;
for (NSUInteger i = 0; i < numberOfPapers; i++) {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(paperWidth * i, 0, paperWidth, aScrollView.bounds.size.height)];
    imageView.image = LOAD_IMAGE_HERE...;
    [aScrollView addSubview:imageView];
}

CGSize contentSize = CGSizeMake(paperWidth * numberOfPapers, aScrollView.bounds.size.height);
aScrollView.contentSize = contentSize;

[self.view addSubview:aScrollView];


来源:https://stackoverflow.com/questions/8777450/ios-horizontally-sliding-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!