標籤:
#import "ViewController.h"#import "YZUIScrollView.h"#define kuan ([UIScreen mainScreen].bounds.size.width+20)#define gao [UIScreen mainScreen].bounds.size.height@interface ViewController ()<UIScrollViewDelegate>@property (weak, nonatomic) IBOutlet UIScrollView *huaBu;@property(nonatomic,strong)NSArray *images;@property(nonatomic)NSInteger currentIndex;@end@implementation ViewController//懶載入,調用get方法時對屬性進行初始化-(NSArray *)images{ if(_images==nil) { NSMutableArray *imagearray=[NSMutableArray array]; for (int i=1; i<7; i++) { NSString *imageName=[NSString stringWithFormat:@"new_feature_%d",i]; UIImage *image=[UIImage imageNamed:imageName]; [imagearray addObject:image]; } _images=imagearray; } return _images;}- (void)viewDidLoad { [super viewDidLoad]; //設定UIScrollView的contentSize _huaBu.contentSize=CGSizeMake(kuan*3, gao); //設定分頁 _huaBu.pagingEnabled=YES; //隱藏水平滾動欄和垂直滾動欄 _huaBu.showsHorizontalScrollIndicator=NO; _huaBu.showsVerticalScrollIndicator=NO; //設定背景顏色,突出不同的圖片 _huaBu.backgroundColor=[UIColor blackColor]; //設定代理要遵守協議<UIScrollViewDelegate> _huaBu.delegate=self; //調用方法添加子視圖 [self tianJiaZiShiTu]; //調用方法添加圖片 [self tianJiaTuPian];}//添加子視圖的方法-(void)tianJiaZiShiTu{ //相簿的話,是一個大的UIScrollView中放了很多的小UIScrollView,但是為了節省記憶體空間,所以只是添加了三個UIScrollView(圖片不停的變換位置) for (int i=0; i<3; i++) { //建立YZUIScrollView YZUIScrollView * yzuisv=[[YZUIScrollView alloc] initWithFrame:CGRectMake(kuan*i, 0, kuan-20, gao)]; //添加YZUIScrollView [_huaBu addSubview:yzuisv]; //設定tag值,便於區分 yzuisv.tag=1000+i; }}//添加方法的圖片-(void)tianJiaTuPian{ YZUIScrollView *leftSC=(YZUIScrollView *)[_huaBu viewWithTag:1000]; YZUIScrollView *middleSC=(YZUIScrollView *)[_huaBu viewWithTag:1001]; YZUIScrollView *rightSC=(YZUIScrollView *)[_huaBu viewWithTag:1002]; leftSC.image=self.images[[self indexFofEnable:_currentIndex-1]]; middleSC.image=self.images[[self indexFofEnable:_currentIndex]]; rightSC.image=self.images[[self indexFofEnable:_currentIndex+1]]; //設定位移量,這步很重要 _huaBu.contentOffset=CGPointMake(kuan, 0);}//確保索引可用-(NSInteger)indexFofEnable:(NSInteger)index{ if(index<0) { return self.images.count-1; } else if (index>self.images.count-1) { return 0; } else return index;}//滾動結束後,把所有的縮放視圖比例還原為1.0-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ for (id obj in _huaBu.subviews) { if([obj isKindOfClass:[UIScrollView class]]) { UIScrollView *scaleSC=(UIScrollView *)obj; scaleSC.zoomScale=1.0; } } //判斷左右滑動 //位移量的x為0,就是說明向右滑動了,就是看的之前左邊的那張圖片 if(scrollView.contentOffset.x==0) { //對應的映像應該是變成左邊的映像 _currentIndex--; } //位移量的x為兩個螢幕的寬,就是說明向左滑動了,就是看的之前右邊的那張圖片 else if(scrollView.contentOffset.x== kuan*2) { //對應的映像應該是變成右邊的映像 _currentIndex++; } _currentIndex=[self indexFofEnable:_currentIndex]; [self tianJiaTuPian];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
第二個類
#import "YZUIScrollView.h"@interface YZUIScrollView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIImage *image;//內容視圖的圖片
@property(nonatomic,strong)UIImageView *imageview;@end@implementation YZUIScrollView-(instancetype)initWithFrame:(CGRect)frame{ if(self =[super initWithFrame:frame]) { //新增內容視圖 UIImageView *imageview1=[[UIImageView alloc] initWithFrame:self.bounds]; [self addSubview:imageview1]; _imageview=imageview1; //設定最大最小倍數和代理 self.minimumZoomScale=0.5; self.maximumZoomScale=1.5; self.delegate=self; //雙擊事件 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shuangJi:)]; tap.numberOfTapsRequired=2; [self addGestureRecognizer:tap]; } return self;}-(void)shuangJi:(UITapGestureRecognizer *)tap{ //當縮放比例不為1.0,還原縮放比例 if (self.zoomScale !=1.0) { [self setZoomScale:1.0 animated:YES]; return ; } CGPoint location =[tap locationInView:self]; CGRect rect =CGRectMake(location.x-100, location.y-100,200,200); [self zoomToRect:rect animated:YES];}//重寫setImg方法-(void)setImage:(UIImage *)image{ //set本身的方法要完成的事必須完成 _image=image; //設定內容視圖的圖片 _imageview.image=image;}//UIScrollViewDelegate代理方法-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return _imageview;}@end
IOS源碼之OC相簿,可以迴圈查看圖片