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

UIScrollView - 常用方法总结

2015-07-23 15:59 477 查看
[color=red]注 : 文章不断更新,转载文章请加上作者[/color]


[size=medium]1 . 什么是UIScrollView ?[/size]

当展示的内容较多, 超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容
而UIScrollView是一个能够滚动的视图控件. 与UIView的最大区别就是[color=red]可滚动[/color]

[size=medium]2. 常用属性[/size]

@property(nonatomic) CGSize  contentSize;//必须设置的属性, 设置滚动区域

@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;// 能否滚动

@property(nonatomic) BOOL bounces;//设置是否需要弹簧效果

@property(nonatomic) BOOL showsHorizontalScrollIndicator;//设置是否显示水平滚动条

@property(nonatomic) BOOL showsVerticalScrollIndicator;[//设置是否显示垂直滚动条

@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled; //设置是否分页 (分页是根据UIScrollView的宽度进行分页)




//这个属性用来表示UIScrollView滚动的位置
//以屏幕显示左上角为圆心,移动contentOffset 位置
@property(nonatomic) CGPoint contentOffset;


//能够在UIScrollView的四周增加额外的滚动区域
//UIEdgeInsets 其实就是一个枚举
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right;
} UIEdgeInsets;
@property(nonatomic) UIEdgeInsets contentInset; [/code]


[size=medium]3. 常用方法[/size]
//(CGPoint)contentOffset : 设置contentOffset ; animated : 是否需要动画 
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;





[size=medium]4. 无法滚动的原因[/size]

-> 没有设置contentSize属性.(没确定滚动范围)
-> 没有取消autolayout功能 , (要想scrolllview滚动, 必须取消autolayout功能).
-> scrollEnabled = NO //能否滚动属性 选择了否
-> 没有接收到触摸事件(继承自UIView中的 userInteractionEnabled属性,)能否跟用户进行交互
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;


[size=medium]5. [color=red]UIScrollView的代理(委托)(delegate)[/color][/size]

delegate,是UIScrollView的一个属性, 当UIScrollView发生一系列的滚动操作时,会自动通知它的代理(delegate)对象, 给它的代理调某个方法发送相应消息,让代理得知它的滚动情况
[color=violet]@property(nonatomic,assign) id<UIScrollViewDelegate> delegate;[/color]
实现代理的条件:
1. 必须是个对象,任意类型都可以
2. 必须遵守UIScrollViewDelegate这个[color=red]协议[/color]的方法

通讯:


//开始拖拽时,调用的方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

//具体滚到到某个位置时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

//用户停止拖拽时
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

//用户使用捏合手势时调用
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;




实现代理三部曲:
5.1. (一般是控制器)遵守协议


//在.m文件中,遵守UIScrollViewDelegate协议
@interface ViewController () <UIScrollViewDelegate>

@end



5.2. 实现方法(具体业务逻辑)


//当滚动开始时,系统自动调用scrollViewWillBeginDragging方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"开始啦~");
}




5.3. 设置UIScrollView的代理(一般控制器为代理)



//将delegate属性设置为当前控制器
self.scrollView.delegate = self;




代理设计模式

1, 让一个对象A监听另一个对象B的状态
2, 一个对象B状态发生了改变,然后通知另一个对象A
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: