[英]iPhone show photos/images sliding (like photo library and Facebook app)?
我幾天前就開始開發iOS了,所以一切對我來說都是新的! 我需要在應用程序中顯示“照片滑塊”,就像我們在iPhone庫或Facebook應用程序中一樣。 經過一番研究,我走到了盡頭。 我的目標是逐個顯示一組照片,並讓用戶從右向左滑動手指,反之亦然:-)
有沒有人有一個例子或知道一個?
謝謝大家。
Well Three20很有名,但我不鼓勵你使用它。 如果您只需要一個照片滑塊,那么將整個Three20框架放入您的項目中,並嘗試弄清楚基於URL的導航工作如何工作可能是一個真正的痛苦。
這是一個更好的選擇, https://github.com/enormego/PhotoViewer 。 它是照片滑塊和相應圖像緩存的獨立實現。 您可以在作者的博客http://developers.enormego.com/上閱讀有關代碼如何工作的更多信息。
您可以使用下面的示例代碼進行圖像幻燈片放映。這里的'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);
}
在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);
}
如果要查看它,可以在Three20中找到“照片滑塊” - https://github.com/facebook/three20/tree/master/samples/TTCatalog - 只需下載源代碼並構建TTCatalog
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.