大家好,今天给大家分享一个简单的全屏浏览图片的Demo.要实现的效果就是点击一个小图,然后全屏浏览这些图片.需要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];//这里动画需要设为NO
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
点击这个小图片之后跳转到第二个ViewController,这里需要把跳转动画设为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.
}
大概就是这个思路,非常简单,大家可以继续完善.也可以给我提些建议,谢谢大家,今天就到这里.☺️
网友评论