#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController<UIScrollViewDelegate>
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)NSMutableArray *images;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, assign)NSInteger flag;
@property (nonatomic, assign)NSInteger number;
@end
#import "MainViewController.h"
#import "RootViewController.h"
@interface MainViewController ()
@property (nonatomic, retain)UIButton *b1;
@end
@implementation MainViewController
- (void)dealloc
{
[super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_flag = 1;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createView];
}
// --------image Array-------
- (void)imageArray
{
self.images = [NSMutableArray array];
for (int i = 0; i < 4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg", i]];
[self.images addObject:image];
}
id imageFirst = [self.images firstObject];
id imageLast = [self.images lastObject];
[self.images insertObject:imageLast atIndex:0]; //把最后一张图片插入到数组第一张
[self.images addObject:imageFirst]; //在数组最后添加第一张图片
self.b1 = [UIButton buttonWithType:UIButtonTypeCustom];
_b1.frame = CGRectMake(0, 0, 100, 50);
_b1.backgroundColor = [UIColor cyanColor];
[_b1 addTarget:self action:@selector(b1:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController addSubview:_b1];
}
-(void)b1:(UIButton *)b1
{
NSLog(@"fgse");
[self.navigationController popViewControllerAnimated:YES];
}
- (void)createView
{
[self imageArray];
self.navigationItem.title = [NSString stringWithFormat:@"第%ld张",self.number + 1];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width * _images.count, 0);// 偏移量为图片个数 * 视图宽度
//***
_scrollView.contentOffset = CGPointMake(self.view.frame.size.width * (self.number + 1), 0);
//***
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
[self.view addSubview:_scrollView];
[_scrollView release];
for (int index = 0; index < _images.count; index++) {
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x + self.view.frame.size.width * index, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2.0;
scrollView.zoomScale = 1.0;
scrollView.delegate = self;
[_scrollView addSubview:scrollView];
[scrollView release];
UIImageView * imageView = [[UIImageView alloc] initWithImage:[self.images objectAtIndex:index]];
imageView.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
imageView.userInteractionEnabled = YES;//交互
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:imageView];
[imageView release];
}
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40)];
_pageControl.backgroundColor = [UIColor whiteColor];
_pageControl.alpha = 0.7;
_pageControl.numberOfPages = [self.images count] - 2;
[_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
_pageControl.pageIndicatorTintColor = [UIColor blackColor];
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
[self.view addSubview:_pageControl];
[_pageControl release];
}
- (void)changePage:(id)sender
{
[_scrollView setContentOffset:CGPointMake((_pageControl.currentPage + 1) * self.scrollView.bounds.size.width, 0) animated:YES];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
if (scrollView != _scrollView){
// 只有小scrollView才允许缩放
return [scrollView.subviews firstObject];
}else{
return nil;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 只要滑动就会被调用
if (scrollView == _scrollView) {
int halfX = scrollView.frame.size.width / 2;
_pageControl.currentPage = (scrollView.contentOffset.x - self.scrollView.bounds.size.width - halfX) / (scrollView.frame.size.width) + 1;
}
}
// 完成缩放
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
UIImageView *imgVC = [scrollView.subviews firstObject];
[imgVC setCenter:CGPointMake(scrollView.frame.size.width / 2, scrollView.frame.size.height / 2 )];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (_scrollView == scrollView) {
//假设flag是当前页面 视图偏移量变化.index不等于flag时 视图还原
NSInteger index = _scrollView.contentOffset.x / self.scrollView.bounds.size.width ;
if (index != _flag) {
UIScrollView *scr = [_scrollView.subviews objectAtIndex:_flag];
scr.zoomScale = 1.0;
_flag = index;
}
//如果视图的偏移量大于最后一张图片位置 ,视图偏移量回到第一张图片位置
if (_scrollView.contentOffset.x >= _scrollView.bounds.size.width * (self.images.count - 1)) {
[_scrollView setContentOffset:CGPointMake(self.scrollView.bounds.size.width, 0) animated:NO];
}
// 如果视图偏移量回到最后一张图片位置
if (_scrollView.contentOffset.x <= 0) {
[_scrollView setContentOffset:CGPointMake(self.scrollView.bounds.size.width * (self.images.count - 2), 0) animated:NO];
}
}
}
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@property (nonatomic, retain)NSMutableArray *images;
@end
#import "RootViewController.h"
#import "MainViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)dealloc
{
[super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Album";
self.view.backgroundColor = [UIColor whiteColor];
[self arrayimage];
NSInteger number;
if (self.images.count % 2== 0) {
number = self.images.count / 2;
} else {
number = self.images.count / 2 + 1;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < number; j++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(self.view.bounds.size.width / 2 * i + 3,
j * self.view.bounds.size.height / 3 +3,
self.view.bounds.size.width / 2 - 3,
self.view.bounds.size.height / 3 - 3);
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg", i + j * 2]] forState:UIControlStateNormal];
[button addTarget:self action:@selector(phontoAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i + j * 2;
[self.view addSubview:button];
}
}
}
// 图片数组
- (void)arrayimage
{
self.images = [NSMutableArray array];
for (int i = 0; i < 4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg", i]];
[self.images addObject:image];
}
}
- (void)phontoAction:(id)sender
{
UIButton *button = (UIButton *)sender;
MainViewController *main = [[MainViewController alloc] init];
main.number = button.tag;
main.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:main animated:YES completion:^{
NSLog(@"进去进去");
}];;
}
相册(代码)
最新推荐文章于 2021-03-15 15:46:44 发布