#import "ViewController.h"
目前成都創(chuàng)新互聯(lián)公司已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、東源網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。#import "YZUIScrollView.h"
#define kuan ([UIScreen mainScreen].bounds.size.width+20)
#define gao [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *huaBu;
@property(nonatomic,strong)NSArray *images;
@property(nonatomic)NSInteger currentIndex;
@end
@implementation ViewController
//懶加載,調(diào)用get方法時對屬性進(jìn)行初始化
-(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];
//設(shè)置UIScrollView的contentSize
_huaBu.contentSize=CGSizeMake(kuan*3, gao);
//設(shè)置分頁
_huaBu.pagingEnabled=YES;
//隱藏水平滾動欄和垂直滾動欄
_huaBu.showsHorizontalScrollIndicator=NO;
_huaBu.showsVerticalScrollIndicator=NO;
//設(shè)置背景顏色,突出不同的圖片
_huaBu.backgroundColor=[UIColor blackColor];
//設(shè)置代理要遵守協(xié)議
_huaBu.delegate=self;
//調(diào)用方法添加子視圖
[self tianJiaZiShiTu];
//調(diào)用方法添加圖片
[self tianJiaTuPian];
}
//添加子視圖的方法
-(void)tianJiaZiShiTu
{
//相冊的話,是一個大的UIScrollView中放了很多的小UIScrollView,但是為了節(jié)省內(nèi)存空間,所以只是添加了三個UIScrollView(圖片不停的變換位置)
for (int i=0; i<3; i++) {
//創(chuàng)建YZUIScrollView
YZUIScrollView * yzuisv=[[YZUIScrollView alloc] initWithFrame:CGRectMake(kuan*i, 0, kuan-20, gao)];
//添加YZUIScrollView
[_huaBu addSubview:yzuisv];
//設(shè)置tag值,便于區(qū)分
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]];
//設(shè)置偏移量,這步很重要
_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;
}
//滾動結(jié)束后,把所有的縮放視圖比例還原為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)
{
//對應(yīng)的圖像應(yīng)該是變成左邊的圖像
_currentIndex--;
}
//偏移量的x為兩個屏幕的寬,就是說明向左滑動了,就是看的之前右邊的那張圖片
else if(scrollView.contentOffset.x== kuan*2)
{
//對應(yīng)的圖像應(yīng)該是變成右邊的圖像
_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 ()
@property(nonatomic,strong)UIImageView *imageview;
@property(nonatomic,strong)UIImage *image;//內(nèi)容視圖的圖片
@end
@implementation YZUIScrollView
-(instancetype)initWithFrame:(CGRect)frame
{
if(self =[super initWithFrame:frame])
{
//添加內(nèi)容視圖
UIImageView *imageview1=[[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:imageview1];
_imageview=imageview1;
//設(shè)置大最小倍數(shù)和代理
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
{
//當(dāng)縮放比例不為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;
//設(shè)置內(nèi)容視圖的圖片
_imageview.image=image;
}
//UIScrollViewDelegate代理方法
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _imageview;
}
@end
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。