试着先去记录一些初级的代码
实现效果:
点击图片看放大图(自己可以在里面添加给图片缩放的功能)
*************************************************************
在第一个ViewController里的代码:
// 创建数组的属性
@property (nonatomic, retain) NSMutableArray *array;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
// 1.创建存放图片的数组
self.array = [NSMutableArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", nil];
// 2.创建一个UIImageView, 让图片从数组中的第一张开始显示(设置下标为0)
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 300, 200)];
img.image = [UIImage imageNamed:[self.array objectAtIndex:0]];
// 3.把图片放到view上
[self.view addSubview:img];
// 4.打开UIImageView的用户交互
img.userInteractionEnabled = YES;
// 5.填充图片(就是只截取图片的中心区域,保证图片不因为UIImageView)的大小而改变尺寸
img.contentMode = UIViewContentModeScaleAspectFill;
// 6.裁掉边框
img.clipsToBounds = YES;
// 7.创建轻拍手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 8.把轻拍手势加在图片上
[img addGestureRecognizer:tap];
}
// 轻拍手势的方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
// 1.push到FirstViewController
FirstViewController *firstVC = [[FirstViewController alloc] init];
// 2.传存图片的数组
firstVC.firstArray = self.array;
// 3.换个模态的样式
[firstVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:firstVC animated:YES completion:^{
}];
}
*****************************************************************************************
在第二个ViewController里的代码:
// 创建接收的数组的属性(且必须在.h里声明,因为要接收前一个页面传来的数组)
@property (nonatomic, retain) NSMutableArray *firstArray;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
// 1.创建轮播图的UIScrollView
UIScrollView *myScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
myScrollV.backgroundColor = [UIColor blackColor];
// 2.设置是否按页滑动
myScrollV.pagingEnabled = YES;
// 3.设置关闭边界反弹
myScrollV.bounces = NO;
// 4.把UIScrollView放到view上
[self.view addSubview:myScrollV];
// 5.设置UIScrollView的滑动范围
myScrollV.contentSize = CGSizeMake(375 * self.firstArray.count, 667);
// 6.遍历数组,取出前面的页面传来的图片,并放在myScrollV上
for (int i = 0; i < self.firstArray.count; i++) {
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(375 * i + 10, 0, 355, 667)];
NSString *imgName = self.firstArray[i];
img.image = [UIImage imageNamed:imgName];
[myScrollV addSubview:img];
// 7.自动适应图片的大小
img.contentMode = UIViewContentModeScaleAspectFit;
}
// 创建轻拍手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[myScrollV addGestureRecognizer:tap];
}
// 轻拍手势的方法:(模态消失)
- (void)tapAction:(UITapGestureRecognizer *)tap
{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
网友评论