我几天前就开始开发iOS了,所以对我来说一切都很新鲜! 我需要在应用程序中显示“照片滑块”,就像我们在iPhone库或Facebook应用程序中一样。 经过一番研究,我走到了尽头。我的目标是逐个显示一组照片,并让用户从右向左滑动手指,反之亦然: - )
有没有人有例子或知道一个?
感谢所有人。
答案 0 :(得分:7)
Well Three20很有名,但我不鼓励你使用它。如果您只需要一张照片滑块,那么将整个Three20框架放入您的项目中,并尝试弄清楚基于URL的导航功能如何工作可能会非常痛苦。
这是一个更好的选择,https://github.com/enormego/PhotoViewer。它是照片滑块和相应图像缓存的独立实现。您可以在作者的博客http://developers.enormego.com/上阅读有关代码如何工作的更多信息。
答案 1 :(得分:5)
你可以使用下面的示例代码进行图片幻灯片放映。这里'scr'是scrollview对象。
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);
}
答案 2 :(得分:1)
在yhpets响应的基础上,通过将标签应用于每个ImageView并使用它们引用每个ImageView并在设备旋转时调整大小来支持方向更改...
在ViewController.h文件中,您需要设置Array和ScrollView ...
@interface ViewController : UIViewController{
NSMutableArray *imgs;
IBOutlet UIScrollView *scr;
}
@property (strong, nonatomic) IBOutlet UIScrollView *scr;
在ViewController.m中,您需要侦听didRotateFromInterfaceOrientation并调整ImageViews的大小以适应新屏幕。我们需要设置screenWidth和screenHeight变量,具体取决于我们是纵向还是横向,并使用它们来调整每个imageview和scrollview的大小。
@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);
}
答案 3 :(得分:0)
如果要查看它,可以在Three20中找到“照片滑块” - https://github.com/facebook/three20/tree/master/samples/TTCatalog - 只需下载源码并构建TTCatalog