大家好,今天給大家分享一個簡單的全屏瀏覽圖片的Demo.要實現(xiàn)的效果就是點擊一個小圖,然后全屏瀏覽這些圖片.需要2個ViewController,廢話不多說,上代碼.
ViewController
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@property(nonatomic, strong)UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 300)];
_imageView.image = [UIImage imageNamed:@"h1.jpg"];
_imageView.userInteractionEnabled = YES;
[self.view addSubview:_imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
[_imageView addGestureRecognizer:tap];
}
-(void)tapAction
{
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:NO];//這里動畫需要設(shè)為NO
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
點擊這個小圖片之后跳轉(zhuǎn)到第二個ViewController,這里需要把跳轉(zhuǎn)動畫設(shè)為NO,然后使第二個ViewController出來的時候透明度從0.2漸變到1,大家可以想象一下效果.
SecondViewController
#import "SecondViewController.h"
#define Width self.view.frame.size.width
#define Height self.view.frame.size.height
@interface SecondViewController ()
@property(nonatomic, strong)UIScrollView *scrollView;
@end
@implementation SecondViewController
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBarHidden = YES;//進入視圖前讓導航條隱藏
/*讓View的透明度漸變到1*/
self.view.alpha = .2;
[UIView animateWithDuration:1 animations:^{
self.view.alpha = 1;
}];
}
-(void)viewWillDisappear:(BOOL)animated
{
/*將要推出時使導航條顯示*/
self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
_scrollView.bounces = NO;
_scrollView.contentSize = CGSizeMake(Width * 7, Height);
_scrollView.contentOffset = CGPointMake(0, 0);
_scrollView.pagingEnabled = YES;
for (NSInteger i = 1; i < 8; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Width * (i - 1), 0, Width, Height)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpg", i]];
imageView.userInteractionEnabled = YES;
[_scrollView addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
[imageView addGestureRecognizer:tap];
}
[self.view addSubview:_scrollView];
}
-(void)tapAction
{
//當點擊圖片的時候,是View的透明度漸變到0.2然后推出,記住不要推出動畫
[UIView animateWithDuration:0.6 animations:^{
self.view.alpha = .2;
} completion:^(BOOL finished) {
[self.navigationController popToRootViewControllerAnimated:NO];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
大概就是這個思路,非常簡單,大家可以繼續(xù)完善.也可以給我提些建議,謝謝大家,今天就到這里.??