标签:
#import "RootView.h"
@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self p_setupView];
}
return self;
}
-(void)p_setupView{
self.backgroundColor = [UIColor yellowColor];
UIImageView *imv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
imv.frame = CGRectMake(0, 0, 150, 150);
// 设置tag值,为了后面缩放的delegate(在controller里)取到imv
imv.tag = 100;
[self addSubview:imv];
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
self.scrollView.backgroundColor = [UIColor greenColor];
// scrollView 属性
// contentsize,能够滑动的决定性因素(能够决定滚动的范围)
self.scrollView.contentSize = imv.frame.size;
// contentOffset 偏移
self.scrollView.contentOffset = CGPointMake(-50, -50);
// 返回顶部
self.scrollView.scrollsToTop = YES;
// pagingEnabled 整页滑动(轮播图,相册)
self.scrollView.pagingEnabled = NO;
// 边界是否反弹
self.scrollView.bounces = YES;
// 是否能够滚动
// self.scrollView.scrollEnabled = NO;
// 是否显示水平滚动条
self.scrollView.showsHorizontalScrollIndicator = NO;
// 是否显示垂直滚动条
self.scrollView.showsVerticalScrollIndicator = YES;
//
self.scrollView.alwaysBounceHorizontal = YES;
self.scrollView.alwaysBounceVertical = YES;
// 缩放,配合代理使用
// 最小0.5倍
self.scrollView.minimumZoomScale = 0.5;
// 最大2倍
self.scrollView.maximumZoomScale = 2;
// 设置缩放比例(是在显示完成之后才有效,在这里设置是无效的)
// self.scrollView.zoomScale = 2;
// 缩放反弹,也就是到最大值,或者最小值的时候,不再能显示放大/缩小的效果
self.scrollView.bouncesZoom = NO ;
// zooming是用来判断缩放状态的,只读
// self.scrollView.zooming
// 将scrollView显示
[self addSubview:_scrollView];
// 将imageView 放在scrollView上
[self.scrollView addSubview:imv];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 通常的用法就是让原来缩放的东西恢复原来的比例,即赋1.
self.scrollView.zoomScale = 0.5;
}
- (void)dealloc
{
[self.scrollView release];
[super dealloc];
}
@end
RootViewController.m
#import "RootViewController.h"
#import "RootView.h"
@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)RootView *rv;
@end
@implementation RootViewController
-(void)loadView{
self.rv = [[[RootView alloc]initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
self.view = _rv;
}
// scrollView 代理方法
// 指定缩放视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [scrollView viewWithTag:100];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 设置代理
self.rv.scrollView.delegate = self;
}
RootViewController.m
#pragma scrollView 的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"已经滚动。。");
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"将要开始拖拽..");
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"已经结束拖拽..");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"将要开始减速..");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"结束减速..");
}
// 缩放有关
// 缩放开始
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [scrollView viewWithTag:100];
}
// 缩放结束
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
}
标签:
原文地址:http://www.cnblogs.com/Coder-GT/p/4878411.html