您好,登錄后才能下訂單哦!
#import "ViewController.h"
#define kuan [UIScreen mainScreen].bounds.size.width
#define gao [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *huaKuang;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//設(shè)置contentSize
_huaKuang.contentSize=CGSizeMake((kuan)*4, gao);
_huaKuang.backgroundColor=[UIColor blackColor];
//設(shè)置分頁(yè)
_huaKuang.pagingEnabled=YES;
//隱藏滾動(dòng)條
//滾動(dòng)時(shí)是否顯示水平滾動(dòng)條
_huaKuang.showsHorizontalScrollIndicator=NO;
//滾動(dòng)時(shí)是否顯示垂直滾動(dòng)條
_huaKuang.showsVerticalScrollIndicator=NO;
//設(shè)置代理,需要遵循代理協(xié)議<UIScrollViewDelegate>,寫(xiě)在@interface ViewController ()的后面
_huaKuang.delegate=self;
//添加子視圖,因?yàn)槭嵌鄠€(gè)所以寫(xiě)了一個(gè)方法來(lái)實(shí)現(xiàn)添加
[self tianJianZiShiTu];
}
-(void)tianJianZiShiTu
{
//假如有六個(gè)圖片,就要?jiǎng)?chuàng)建六個(gè)UIScrollView和六個(gè)UIImageView,并且找到六個(gè)圖片
for(int i=0;i<3;i++)
{
//創(chuàng)建UIScrollView
//為了區(qū)分開(kāi)不同的照片加一個(gè)20黑邊,需要設(shè)置ScrollViewscroll View中的Left和View中的Width
UIScrollView *uisv=[[UIScrollView alloc] initWithFrame:CGRectMake((kuan+20)*i, 0, kuan, gao)];
//把創(chuàng)建完成的添加到總的那個(gè)UIScrollView上
[_huaKuang addSubview:uisv];
//創(chuàng)建UIImageView
UIImageView *uiiv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kuan, gao)];
//把創(chuàng)建的UIImageView添加到UIScrollView中
[uisv addSubview:uiiv];
//設(shè)置UIImageView的圖片
NSString *imageName = [NSString stringWithFormat:@"new_feature_%d",i + 1];
uiiv.image=[UIImage imageNamed:imageName];
//設(shè)置tag值
uiiv.tag=1000;
//設(shè)置UIScrollView的代理
uisv.delegate=self;
//設(shè)置縮放范圍
uisv.minimumZoomScale=0.5;
uisv.maximumZoomScale=1.5;
//定義點(diǎn)擊事件
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianJiShiJian:)];
//設(shè)置有效點(diǎn)擊數(shù)(就是雙擊)
tap.numberOfTapsRequired=2;
//添加到UIScrollView中
[uisv addGestureRecognizer:tap];
}
}
-(void)dianJiShiJian:(UITapGestureRecognizer *)tap
{
//獲取點(diǎn)擊事件的view
UIScrollView *uisv1=(UIScrollView *)tap.view;
if(uisv1.zoomScale!=1.0)
{
[uisv1 setZoomScale:1.0 animated:YES];
return ;
}
CGPoint location=[tap locationInView:tap.view];
CGRect rect=CGRectMake(location.x-100, location.y-100, 200, 200);
[uisv1 zoomToRect:rect animated:YES];
}
//指定縮放的視圖
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if (scrollView == _huaKuang) {
return nil;
}
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:1000];
return imageView;
}
//滾動(dòng)結(jié)束,把所有的縮放視圖的縮放比例置為1.0
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView == _huaKuang) {
for (id obj in _huaKuang.subviews) {
if ([obj isKindOfClass:[UIScrollView class]]) {
UIScrollView *scaleSC = (UIScrollView *)obj;
scaleSC.zoomScale = 1.0;
}
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。