您的位置:首页 > 其它

可以循环播放, 所放的的相册

2016-01-10 21:18 513 查看
#import "AlbumPlusViewController.h"

#define kwidth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height

@interface AlbumPlusViewController () <UIScrollViewDelegate> {
UIScrollView *bigScrollView;
}

@end

@implementation AlbumPlusViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"ScrollView上放相册";

//解题思路: 可视区域,看成是可以移动的; 第一张放最后一张, 最后一张放第一张; 刚开始可视区域移动在第二位(偏移量为宽度一倍, 在该位置放将要播放的图片; 关于缩放比例:先进行判断, 不让大的视图发生改变, 获取小的视图,再返回视图; 关于偏移量改变. 如果, 偏移量为最后一张图片, 偏移量改为第一张,(当然刚开始, 图片就要设置好)),当偏移量为第一张, 就改为最后一张; 所以视图需要放容下图片总个数加2, 当用户翻到第一页的前一张, 可以看到, 同时又让其偏移量回到最后一张, 实现循环播放的效果
//大视图
//可视区域
//坐标都是基于父类
bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kwidth, kheight)];
bigScrollView.contentSize = CGSizeMake(8 * kwidth, 0);
//大的视图偏移量是宽度(内容页的偏移量)
bigScrollView.contentOffset = CGPointMake(kwidth, 0);
bigScrollView.delegate = self;
bigScrollView.pagingEnabled = YES;
[self.view addSubview:bigScrollView];

//小视图
for (NSInteger i = 0; i < 8; i++) {
//坐标基于大的视图
UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(kwidth * i, 0, kwidth, kheight)];
smallScrollView.delegate = self;
smallScrollView.tag = 200 + i;
smallScrollView.maximumZoomScale = 2;
smallScrollView.minimumZoomScale = 0.5;
[bigScrollView addSubview:smallScrollView];

//imageView是放在scrollView上的
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kwidth, kheight)];
imageview.tag = 999;
NSString *name = [NSString stringWithFormat:@"%ld.jpg", i];
imageview.image = [UIImage imageNamed:name];
[smallScrollView addSubview:imageview];
}

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

//总的
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView == bigScrollView) {
CGFloat a = scrollView.contentOffset.x;
//如果改为0, 那么,改变其为kwidth * 5
if (a == 0) {
bigScrollView.contentOffset = CGPointMake(kwidth * 6, 0);
}

if (a == kwidth * 7) {
bigScrollView.contentOffset = CGPointMake(0, 0);
}

NSInteger currentPage = scrollView.contentOffset.x / kwidth;
UIScrollView *smallScrollView = [scrollView viewWithTag:currentPage + 200];
[smallScrollView setZoomScale:1.0 animated:NO];

}

}

- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
if (scrollView == bigScrollView) {
return nil;
} else {
return [scrollView viewWithTag:999];
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: