xcode iOS:如何在打开分页的图像中滑动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9440444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: How To swipe through an image with pagination on
提问by Sneagan
Basically, I want to have an app with only one view that has an image on it. I want to be able to swipe left or right and have the first image go out of the view and the second image to come in. The images are the same and I want it to look like they are connected (like scrolling down a rope where the pattern just repeats, but it looks like a constant scroll). I need it to be able to change the image or restart after a series of swipes. I know that I need to turn pagination ON in the UIScrollView, but I am new to iOS and am having trouble.
基本上,我想要一个只有一个视图的应用程序,上面有一个图像。我希望能够向左或向右滑动,让第一个图像离开视图,第二个图像进入。图像是相同的,我希望它看起来像它们是连接的(就像向下滚动一根绳子模式只是重复,但它看起来像一个不断的滚动)。我需要它能够在一系列滑动后更改图像或重新启动。我知道我需要在 UIScrollView 中打开分页,但我是 iOS 新手并且遇到了问题。
Ultimately, I want to have the iPhone vibrate every so-and-so swipes (and restart the pattern).
最终,我想让 iPhone 在每一次这样的滑动(并重新启动模式)时振动。
I'm sure that there are a lot of ways to do this (i.e. a TableView) so feel free to just point me in the direction of some references if the answer is tedious to explain.
我确信有很多方法可以做到这一点(即 TableView),所以如果答案解释起来很乏味,请随意指出一些参考文献的方向。
Thanks!
谢谢!
FOLLOW UP:
跟进:
I found an Apple example that did very nearly what I wanted to do. I made a lot of adjustments to it, but I'm banging my head against a wall trying to get the images to cycle. Here is what I think is the offending code, but I'm not sure what the solution is, as the ScrollView is functional, it just doesn't reset the center to the current view. Any ideas?
我发现了一个 Apple 示例,它几乎可以完成我想做的事情。我对它做了很多调整,但我正用头撞墙试图让图像循环。这是我认为有问题的代码,但我不确定解决方案是什么,因为 ScrollView 是功能性的,它只是不会将中心重置为当前视图。有任何想法吗?
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
回答by Aaron Hayman
I'd just use a UIScrollView. Set the contentWidth to be 3 times the width/height of the view (for 3 pages) and set the contentOffset to be the center 'page' (view.bounds.size.width
or view.bounds.size.height
depending on whether you're scrolling horizontally/vertically respectively) . You'll need to setup a delegate for the UIScrollView (probably the view controller) and implement - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
. This will be called when the scroll view has finished decelerating. Once it has finished decelerating, reset the contentOffset back to the center view. This should give the impression of an infinite scroll. You can also set a counter to increment in the scrollViewDidEndDecelerating method to increment the counter or initiate the vibration.
我只是使用 UIScrollView。将 contentWidth 设置为视图宽度/高度的 3 倍(对于 3 个页面)并将 contentOffset 设置为中心“页面”(view.bounds.size.width
或view.bounds.size.height
取决于您是否分别水平/垂直滚动)。您需要为 UIScrollView(可能是视图控制器)设置一个委托并实现- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
. 这将在滚动视图完成减速时调用。完成减速后,将 contentOffset 重置回中心视图。这应该给人一种无限滚动的印象。您还可以在 scrollViewDidEndDecelerating 方法中设置一个计数器递增,以递增计数器或启动振动。
You shouldn't need to keep repositioning the images. Just set the images once in the scrollView:
您不需要不断重新定位图像。只需在 scrollView 中设置一次图像:
//Horizontal arrangement
UIImage *image = [UIImage imageNamed:@"nameOfImage.png"];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image];
UIImageView *imageView3 = [[UIImageView alloc] initWithImage:image];
NSArray *imageViews = [NSArray arrayWithObjects:imageView1, imageView2, imageView3];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: scrollView]; //This code assumes it's in a UIViewController
CGRect cRect = scrollView.bounds;
UIImageView *cView;
for (int i = 0; i < imageViews.count; i++){
cView = [imageViews objectAtIndex:i];
cView.frame = cRect;
[scrollView addSubview:cView];
cRect.origin.x += cRect.size.width;
}
scrollView.contentSize = CGSizeMake(cRect.origin.x, scrollView.bounds.size.height);
scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0); //should be the center page in a 3 page setup
So the images are setup, you don't need to mess with them anymore. Just reset the contentOffset when the scroll views stops (note: you need to make sure you're the delegate of the scroll view or you'll not receive the message when the scroll view stops):
这样图像就设置好了,你不需要再弄乱它们了。只需在滚动视图停止时重置 contentOffset(注意:您需要确保您是滚动视图的代表,否则在滚动视图停止时您将不会收到消息):
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
}
Please forgive any typos. I wrote it out by hand.
请原谅任何错别字。我亲手写的。
回答by CodaFi
Look on cocoacontrols.comfor a custom photo album view. As for the vibration, this code snippet vibrates the phone (make sure you link to and #import <AudioToolbox/AudioToolbox.h>
):
在cocoacontrols.com 上查看自定义相册视图。至于振动,此代码段会使手机振动(确保链接到 和#import <AudioToolbox/AudioToolbox.h>
):
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);