您的位置:首页 > 产品设计 > UI/UE

ioS开发之UI基础--UIScrollView的基本使用

2015-09-03 22:43 561 查看
UIScrollView的基本使用

一、使用步骤

1、拖拽一个UIScrollView

2、把滚动的内容放到UIScrollView中

3、设置UIScrollView的contentSize属性
拖动的范围

二、无法滚动检查

//以上步骤即可,如果控件依然无法滚动,检查下面两个属性是否被禁用

1、self.scrollView.scrollEnabled;

2、self.scrollView.userInteractionEnabled;

三、滚动大图的练习

<span style="font-size:18px;">self.scrollView.contentSize = self.imageView.frame.size;
//self.scrollView.contentInset = UIEdgeInsetsMake(10, 20, 30,40);
//self.scrollView.contentOffset = CGPointMake(-100, -20);
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.bounces = NO;
- (IBAction)btnClick {
CGPoint point = self.scrollView.contentOffset;point.x += 50;
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentOffset = point;
}];
}</span>


动画的方式滚动大图

主要理解: frame contentSize contentOffset contentInset

CGPoint point = self.scrollView.contentOffset;
point.x += 50;
[UIView animateWithDuration:1.0 animations:^{
self.scrollView.contentOffset = point;
}];
self.scrollView.contentSize = self.imageView.frame.size;
//self.scrollView.contentInset = UIEdgeInsetsMake(10, 20, 30,40);
//self.scrollView.contentOffset = CGPointMake(-100, -20);


UIScrollView的常见属性

<span style="font-size:18px;">UIScrollView常用属性
//滚动的位置 屏幕左上角的位置相对于图片左上角的位置,图片左上角的位置为0,0
//scrollView相对于内容的偏移
@property(nonatomic) CGPoint contentOffset;
// default CGPointZero
//滚动范围
@property(nonatomic) CGSize contentSize;// default CGSizeZero
//scrollView的大小(可视范围)self.scrollView.frame
//上下左右,逆时针顺序,增加边距。默认不显示这个距离,滚动之后才有
@property(nonatomic) UIEdgeInsets contentInset;
// default UIEdgeInsetsZero. add additional scroll area around content
//是否启用弹簧效果。默认启用
@property(nonatomic) BOOL bounces;
// default YES. if YES, bounces past edge of content and back again
//启用滚动
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
// default YES. turn off any dragging temporarily
//横向滚动条
@property(nonatomic) BOOL showsHorizontalScrollIndicator;
// default YES. show indicator while we are tracking. fades out after tracking
//纵向滚动条
@property(nonatomic) BOOL showsVerticalScrollIndicator;
// default YES. show indicator while we are tracking. fades out after tracking </span>


UIScrollViewDelegate协议

当UIScrollView发生一系列的滚动操作时, 会自动通知它的代理(delegate)对象,给它的代理发送相应的消息,让代理得知它的滚动
情况

也就是说,要想监听UIScrollView的滚动过程,就必须先给UIScrollView设置一个代理对象,然后通过代理得知UIScrollView的滚动过程

一、设置scrollView的代理

1、先让controller遵守UIScrollViewDelegate协议

2、设置scrollView的代理为controller

3、设置代理的方法(代理的名字都以类的名字开始,代理的方法也是
以对象的名字开始)

#pragma mark - scrollView的代理方法
-  (void)scrollViewWillBeginDragging:(UIScrollView *) scrollView{
NSLog(@"马上开始滚了");}
-  (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//scrollView.contentOffset
NSLog(@"别打扰我,正在滚");}


当用户在UIScrollView中使用捏合手势时,UIScrollView会给代理发送一条消息,询问代理要缩放自己内部的哪一个子控件(哪一 块内容)UIScrollView只能缩放一个控件

代理中缩放的消息

viewForZoomingInScrollView

1.实现捏合手势放大缩小的步骤

<span style="font-size:18px;">//设置代理
self.scrollView.delegate = self;
//设置最大和最小的缩放比例
self.scrollView.maximumZoomScale = 2.0;
self.scrollView.minimumZoomScale = 0.5;
</span>


2、调用代理的方法

<span style="font-size:18px;">/**
* 当用户使用捏合手势的时候调用
*
* @param scrollView
*
* @return 返回的就是要缩放的是哪一个控件
*/
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.imageView;
}
</span>


代理的作用

1、监听的思想:让一个对象a
监听另一个对象b的状态改变

2、通知的思想:一个对象b状态改变(做了某些事情)
通知另一个对象2、代理中的

@optional
可选的如果代理中的方法没有用它修饰的话,会有警告,调用方法的时候要判断代理是否提供了此方法

@required
必须的 (默认)如果代理中的方法用它修饰的话,会有警告,调用方法的时候要判断代理是否提供了此方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: