#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, WIDTH-20, HEIGHT - 30)];
scroll.backgroundColor = [UIColor whiteColor];
scroll.contentSize = CGSizeMake((WIDTH-20)*2, HEIGHT+30);//设置滚动范围,如果想要左右滑动,则必须保证此宽大于scroll的frame对应的宽,如果想要上下滑动必须保证此高大于scroll的frame对应的高
scroll.scrollEnabled = NO;//是否允许滚动(默认允许)
scroll.bounces = NO; //是否有弹簧效果(默认有)
scroll.contentOffset = CGPointMake(20, 40);//设置scrollview滚动到某个位置
NSLog(@"%@",NSStringFromCGPoint(scroll.contentOffset));//获取scrollview当前滚动的位置
scroll.pagingEnabled = YES;//是否允许整页滚动,如果想要左右整页滑动,要保证contentsize的宽是scrollview frame宽的整数倍,如果要上下整页滑动,要保证contentsize 的高是frame高的整数倍,当下一页露出范围小于整页的一半时,滚回到当前页,当超出一半时,滚动到下一页
scroll.showsHorizontalScrollIndicator = NO;//是否显示水平滚动条
scroll.showsVerticalScrollIndicator = NO;//是否显示垂直滚动条
scroll.scrollsToTop = YES;//点状态栏时,是否允许scorllView滚动到顶部(默认允许)
scroll.zooming = YES;//是否允许缩放(代理方法)
scroll.zoomScale = 2;//缩放比例
[self.view addSubview:scroll];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, HEIGHT, 200, 30)];
label.backgroundColor = [UIColor redColor];
[scroll addSubview:label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@interface ViewController ()<UIScrollViewDelegate>
{
UISegmentedControl *segment;
UIScrollView *scrollView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 30, 300, 400)];
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.contentSize = CGSizeMake(900, 400);//设置滚动的范围
scrollView.delegate = self;
scrollView.pagingEnabled = YES;//是否允许整页滑动
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
view.backgroundColor = [UIColor grayColor];
[scrollView addSubview:view];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(300, 0, 300, 400)];
view1.backgroundColor = [UIColor brownColor];
[scrollView addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(600, 0, 300, 400)];
view2.backgroundColor = [UIColor purpleColor];
[scrollView addSubview:view2];
segment = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2",@"3"]];
segment.frame = CGRectMake(10, 450,200, 40);
segment.selectedSegmentIndex = 0;
[segment addTarget:self action:@selector(segmentChange) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];
}
-(void)segmentChange{
CGPoint p = {segment.selectedSegmentIndex*300,0};
[scrollView setContentOffset:p animated:YES];
// NSLog(@"滑动时调用");
//点击状态栏调用(scrolltotop = yes)
// NSLog(@"到顶了");
}
//手指放到scrollview上开始滑动时调用
// NSLog(@"调用");
}
//停止拖拽
// NSLog(@"--->停止");
//停止减速--scrollview不动了
// NSLog(@"不动了");
CGPoint p = scrollView.contentOffset;
float w = p.x;
int index = w/scrollView.frame.size.width;
NSLog(@"当前:%d页",index);
segment.selectedSegmentIndex = index;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@interface ViewController ()<UIScrollViewDelegate>{
UIScrollView *_scrollView;
UIImageView *_imgView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.delegate = self;
_scrollView.minimumZoomScale = 1;//缩放的最小比例
_scrollView.maximumZoomScale = 3;//缩放的最大比例
[self.view addSubview:_scrollView];
_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
_imgView.image = [UIImage imageNamed:@"1.jpg"];
[_scrollView addSubview:_imgView];
}
//告诉scrollView,是哪个子视图要进行缩放
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return _imgView;
}
//停止缩放调用的方法
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
//停止缩放
NSLog(@"调用");
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
NSLog(@"---");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
转自:http://thenewself.blog.163.com/blog/static/198501330201221862929331/
实现UIScrollView的缩放,必须使maximumZoomScale(默认1.0)和minimumZoomScale(默认1.0)不同 ,并且需要在delegate中的viewForZoomingInScrollView: 方法中返回需要所放的view。实现以上即可进行缩放。
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
CGFloat xcenter = scrollView.center.x , ycenter = scrollView.center.y;
//目前contentsize的width是否大于原scrollview的contentsize,如果大于,设置imageview中心x点为contentsize的一半,以固定imageview在该contentsize中心。如果不大于说明图像的宽还没有超出屏幕范围,可继续让中心x点为屏幕中点,此种情况确保图像在屏幕中心。
xcenter = scrollView.contentSize.width > scrollView.frame.size.width ?
scrollView.contentSize.width/2 : xcenter;
//同上,此处修改y值
ycenter = scrollView.contentSize.height > scrollView.frame.size.height ?
scrollView.contentSize.height/2 : ycenter;
[[scrollView viewWithTag:imageview] setCenter:CGPointMake(xcenter, ycenter)];
}
设置scrollview的bouncesZoom属性可以确保view的放缩比例超出设置比例范围时自动进行反弹。
使图片自适应屏幕大小,以确保图片在屏幕正中的方法:
float x_scale = scrollView.frame.size.width/selectedImage.size.width;
float y_scale = scrollView.frame.size.height/selectedImage.size.height;
CGFloat scale = x_scale < y_scale ? x_scale : y_scale;
imageView.transform = CGAffineTransformMakeScale(scale, scale);