標籤:
現在你只要拿著手機,不管你Android還是iOS,新聞類的App不可避免都有一個功能就是圖片查看,做個專題,查看一下內容,App Store中也有專門針對圖片瀏覽的App,鑒於目前所知有限,無法做到那麼高大上的App,簡單的做個美女查看的Demo。沒有太多的功能,上一張,下一張,標籤,圖片,簡簡單的,深刻的感覺到知識就是力量,目前知識有限的結果就是Demo簡單,且學且珍惜吧。
1.建立項目(如果不會可以參考本人之前的文章),然後在StoryBoard中進行布局,將Girl檔案夾中的圖片拖入項目中;
2.將UIImageView,UILabel,UIButton拖入StoryBoard中,並且設定背景圖片;
設定背景圖片:
3.ViewController.h中定義成員變數:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (weak, nonatomic) IBOutlet UILabel *pageLabel;@end
4.上一張和下一張的事件代碼:
定義一個圖片數組:
@interface ViewController ()@property NSArray *imageArr;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [email protected][@"girl0.jpg",@"girl1.jpg",@"girl2.jpg"];}
上一張的代碼:
//顯示上一張- (IBAction)preview:(id)sender { NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue]; NSInteger allCount=[_imageArr count]; if (currentIndex==1) { currentIndex=allCount; }else{ currentIndex=currentIndex-1; } //設定標籤 [_pageLabel setText:[NSString stringWithFormat:@"%ld/%ld",currentIndex,(long)allCount]]; //擷取圖片的名稱 NSString *imageName=[NSString stringWithFormat:@"girl%ld.jpg",(long)currentIndex-1]; UIImage *image=[UIImage imageNamed:imageName]; [_imageView setImage:image]; }
下一張代碼:
//顯示下一張- (IBAction)nextView:(id)sender { //截取標籤上面的數字 NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue]; NSInteger allCount=[_imageArr count]; if (currentIndex==allCount) { currentIndex=0; } NSString *imageName=[NSString stringWithFormat:@"girl%ld.jpg",(long)currentIndex]; [_pageLabel setText:[NSString stringWithFormat:@"%ld/%ld",currentIndex+1,(long)allCount]]; UIImage *image=[UIImage imageNamed:imageName]; [_imageView setImage:image]; }
以上的代碼基本上都是OC基礎,UIImage設定圖片的時候只需要傳遞一片名稱即可,不需要傳遞路徑;
5.最終效果如下:
效果很簡單,就是在上一張和下一張到臨界點的時候判斷一下,兩者的代碼類似,其實可以封裝一下,周末愉快;
ViewController.m中的代碼:
//// ViewController.m// MyPicture//// Created by keso on 15/1/17.// Copyright (c) 2015年 keso. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property NSArray *imageArr;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [email protected][@"girl0.jpg",@"girl1.jpg",@"girl2.jpg"];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}//顯示上一張- (IBAction)preview:(id)sender { NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue]; NSInteger allCount=[_imageArr count]; if (currentIndex==1) { currentIndex=allCount; }else{ currentIndex=currentIndex-1; } //設定標籤 [_pageLabel setText:[NSString stringWithFormat:@"%ld/%ld",currentIndex,(long)allCount]]; //擷取圖片的名稱 NSString *imageName=[NSString stringWithFormat:@"girl%ld.jpg",(long)currentIndex-1]; UIImage *image=[UIImage imageNamed:imageName]; [_imageView setImage:image]; }//顯示下一張- (IBAction)nextView:(id)sender { //截取標籤上面的數字 NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue]; NSInteger allCount=[_imageArr count]; if (currentIndex==allCount) { currentIndex=0; } NSString *imageName=[NSString stringWithFormat:@"girl%ld.jpg",(long)currentIndex]; [_pageLabel setText:[NSString stringWithFormat:@"%ld/%ld",currentIndex+1,(long)allCount]]; UIImage *image=[UIImage imageNamed:imageName]; [_imageView setImage:image]; }@end
iOS開發-簡單的圖片查看器