xcode iPhone 显示照片/图像滑动(如照片库和 Facebook 应用程序)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4777122/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:40:42  来源:igfitidea点击:

iPhone show photos/images sliding (like photo library and Facebook app)?

iphonexcodeimageslide

提问by Hugo

I started developing to iOS just a couple of days ago, so everything is very new to me! I need to show in an application a "photo slider" as we've in the iPhone library or Facebook application. After some research, I reached a dead end. My goal is to show a set of photos, one by one, and having the user slide the finger from right to left, or vice versa :-)

几天前我才开始开发 iOS,所以一切对我来说都是全新的!我需要在应用程序中显示“照片滑块”,就像我们在 iPhone 库或 Facebook 应用程序中一样。经过一番研究,我走到了死胡同。我的目标是一张一张地显示一组照片,并让用户从右向左滑动手指,反之亦然:-)

Does anyone has an example or knows of one?

有没有人有一个例子或知道一个?

Thanks to all.

谢谢大家。

回答by Di Wu

Well Three20 is famous but I would discourage you from using it. If all you need is a photo slider then putting the entire Three20 framework into your project and trying to figure out how the URL-based navigation thing works can be a real pain.

好吧 Three20 很有名,但我不鼓励你使用它。如果您只需要一个照片滑块,那么将整个 Three20 框架放入您的项目中并试图弄清楚基于 URL 的导航是如何工作的可能会很痛苦。

Here is a better alternative, https://github.com/enormego/PhotoViewer. It's a standalone implementation of the photo slider and a corresponding image cache. You can read more about how the code works on the author's blog, http://developers.enormego.com/.

这是一个更好的选择,https://github.com/enormego/PhotoViewer。它是照片滑块和相应图像缓存的独立实现。您可以在作者的博客http://developers.enormego.com/上阅读有关代码如何工作的更多信息。

回答by yhpets

you can use below sample code for image slideshow..Here 'scr' is the scrollview object.

您可以使用以下示例代码进行图像幻灯片放映。这里的“scr”是滚动视图对象。

NSMutableArray *imgs=[[NSMutableArray  alloc]init]; 
                         // arrayWithObjects:[UIImage @"abc1.jpg"],[UIImage @"abc2.jpg"],[UIImage @"abc3.jpg"], [UIImage@"abc4.jpg"],
//                [ UIImage @"abc5.jpg"],nil];

                      [imgs addObject:[UIImage imageNamed:@"abc1.jpg"]];
                      [imgs addObject:[UIImage imageNamed:@"abc2.jpg"]];
                       [imgs addObject:[UIImage imageNamed:@"abc3.jpg"]];
                        [imgs addObject:[UIImage imageNamed:@"abc4.jpg"]];
                         [imgs addObject:[UIImage imageNamed:@"abc5.jpg"]];
for(int i=0;i<imgs.count;i++)
{
    CGRect frame;
    frame.origin.x=self.scr.frame.size.width *i;
    frame.origin.y=0;
    frame.size=self.scr.frame.size;
    UIImageView *subimg=[[UIImageView alloc]initWithFrame:frame];
    subimg.image=[imgs objectAtIndex:i];
    [self.scr  addSubview:subimg];
    [subimg release];
    self.scr.contentSize=CGSizeMake(self.scr.frame.size.width*imgs.count, self.scr.frame.size.height);

}

回答by Jim True

Building on yhpets response, this supports orientation changes too by applying tags to each ImageView and using them to reference each ImageView and resize when the device is rotated...

建立在 yhpets 响应的基础上,这也通过将标签应用于每个 ImageView 并使用它们来引用每个 ImageView 并在设备旋转时调整大小来支持方向更改...

In your ViewController.h file you'll need to set up your Array and ScrollView...

在您的 ViewController.h 文件中,您需要设置您的 Array 和 ScrollView...

@interface ViewController : UIViewController{
    NSMutableArray *imgs;
    IBOutlet UIScrollView *scr;
}
@property (strong, nonatomic) IBOutlet UIScrollView *scr;

And in your ViewController.m you need to listen for didRotateFromInterfaceOrientation and resize the ImageViews to fit the new screen. We need to set the screenWidth and screenHeight variables depending on if we are portrait or landscape and use these to resize each imageview and the scrollview.

在您的 ViewController.m 中,您需要监听 didRotateFromInterfaceOrientation 并调整 ImageViews 的大小以适应新屏幕。我们需要根据我们是纵向还是横向来设置 screenWidth 和 screenHeight 变量,并使用它们来调整每个图像视图和滚动视图的大小。

@implementation ViewController
@synthesize scr;

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenWidth = screenRect.size.width;
        CGFloat screenHeight = screenRect.size.height;
    if ((self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)||(self.interfaceOrientation == UIInterfaceOrientationPortrait)){
//screenWidth and screenHeight are correctly assigned to the actual width and height
    }else{
        screenHeight = screenRect.size.width;
        screenWidth = screenRect.size.height;
        screenRect.size.width = screenWidth;
        screenRect.size.height = screenHeight;
    }
    NSLog(@"see it's right now= (%f,%f)",screenWidth,screenHeight);
    self.scr.contentSize=CGSizeMake(screenWidth*imgs.count, screenHeight);
    for(int i=0;i<imgs.count;i++){
        UIImageView *targetIV = (UIImageView*)[self.view viewWithTag:(i+1)];
        CGRect frame;
        frame.origin.x=screenWidth *i;
        frame.origin.y=0;
        frame.size=CGSizeMake(screenWidth, screenHeight);
        targetIV.frame = frame;
    }
}///////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    imgs=[[NSMutableArray  alloc]init];
    [imgs addObject:[UIImage imageNamed:@"1001.png"]];
    [imgs addObject:[UIImage imageNamed:@"1002.png"]];
    [imgs addObject:[UIImage imageNamed:@"1003.png"]];
    [imgs addObject:[UIImage imageNamed:@"1004.png"]];
    for(int i=0;i<imgs.count;i++){
        CGRect frame;
        frame.origin.x=self.scr.frame.size.width *i;
        frame.origin.y=0;
        frame.size=self.scr.frame.size;
        UIImageView *subimg=[[UIImageView alloc]initWithFrame:frame];
        subimg.image=[imgs objectAtIndex:i];
        subimg.tag = (i+1);
        subimg.contentMode = UIViewContentModeScaleAspectFit;
        [self.scr  addSubview:subimg];
    }
    self.scr.contentSize=CGSizeMake(self.scr.frame.size.width*imgs.count, self.scr.frame.size.height);
}

回答by timothy5216

There's "photo slider" in Three20 if you want to check it out - https://github.com/facebook/three20/tree/master/samples/TTCatalog- just download the source and build TTCatalog

如果您想查看它,Three20 中有“照片滑块” - https://github.com/facebook/three20/tree/master/samples/TTCatalog- 只需下载源代码并构建 TTCatalog