2011-01-23 161 views
3

幾天前我開始開發iOS,所以對我來說一切都是新的! 我需要在應用程序中顯示「照片滑塊」,就像我們在iPhone庫或Facebook應用程序中一樣。 經過一番研究,我達到了死衚衕。我的目標是逐個展示一組照片,讓用戶從右向左滑動手指,反之亦然:-)iPhone顯示照片/圖片滑動(如照片庫和Facebook應用程序)?

有沒有人有一個例子或知道一個?

感謝所有。

回答

7

好Three20是有名的,但我會使用它阻止你。如果您只需要一個照片滑塊,然後將整個Three20框架放入您的項目中,並試圖弄清楚基於URL的導航工具的工作原理可能是一個真正的痛苦。

這是一個更好的選擇,https://github.com/enormego/PhotoViewer。它是獨立實現的照片滑塊和相應的圖像緩存。您可以閱讀更多關於代碼如何在作者博客http://developers.enormego.com/上工作的信息。

+1

是的,我想說的是關於Three20的同樣的事情。基於URL的導航是一個真正的痛苦。 – timothy5216 2011-01-24 01:28:03

5

你可以使用下面的示例代碼進行圖像幻燈片..這裏'scr'是scrollview對象。在yhpets響應

NSMutableArray *imgs=[[NSMutableArray alloc]init]; 
         // arrayWithObjects:[UIImage @"abc1.jpg"],[UIImage @"abc2.jpg"],[UIImage @"abc3.jpg"], [[email protected]"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); 

} 
1

大廈,這支持方向通過應用標籤每個ImageView的和使用它們引用每個ImageView的,當設備旋轉調整也改變...

在你ViewController.h文件你需要設置你的陣列和滾動型...

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

而在你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); 
}