scrollView之添加随机颜色或者图片

来源:互联网 发布:kenzo男士香水 知乎 编辑:程序博客网 时间:2024/04/30 04:50

首先对scrollView作一下简介:

<span style="font-size:12px;">1. 什么是UIScrollView?可滚动的view2. UIScrollView为什么要滚动? 滚动到哪里?一个屏幕不能完整的显示内容显示的内容有多大, 就滚动到内容的边界3. 什么在滚动?scrollView 的内容(content) , 要添加到scrollView上3.1 怎么添加scrollView?1> 拖放UIScrollView控件到控制器view上2> 添加scrollView的content(子view)3> 设置contentSize4. UIScrollView常用属性contentSize : 必须要大与scrollView的宽高contentInset: 内边距, 距离scrollView边框的一个距离, 如果拖动之后, content会停在设置边距的那个位置contentOffset: 偏移量, content滚动到的位置bounce : 弹簧效果, 默认开启 , 如果设置为NO, 那么将不会有弹簧的效果// 即使不设置 contentSize , 也会有弹簧效果, 但是 bounce 属性不能设置为NOalwaysBounceHorizontalalwaysBounceVertical// 水平和垂直方向上的指示器showsHorizontalScrollIndicatorshowsVerticalScrollIndicator5. ScrollView的size和contentSize的区别?contentSize : 内容的可滚动范围size: scrollView本身的 大小6.1 无法滚动的原因1. contentSize 设置的比 scrollView的size 小的时候,比可以滚动2. userInteractionEnabled = NO; 的时候, 用户不能交互3. scrollEnabled 设置为NO的时候, 将不能滚动7. 图片轮播器和pageControllUIScrollView 和 UIPageControl// pageEnabled 分页效果 , 依据scrollView的宽度来分页_scrollView.pagingEnabled = YES;8.UIPageControlnumberOfPage 有多少页currentPage 当前在第几页// 设置页码指示器的颜色_pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];// 设置当前页码指示器的颜色_pageControl.currentPageIndicatorTintColor = [UIColor redColor];scrollView 在和 pageControl 结合时候的时候- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {// 计算当前是第几张图片NSInteger currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;// 设置 pagecontorl的 当前页码_pageControl.currentPage = currentPage;}12. 自动播放NSTimer 计时器_timer = [NSTimer scheduledTimerWithTimeInterval:1 // 间隔target:self // 控制器selector:@selector(changePage) // 调用的方法userInfo:nil // 传递的参数, 如果上面方法需要参数, 就在这里传递repeats:YES]; // 是否重复timer 创建完成之后, 就直接生效invalidate 使计时器无效, 再启用计时器就需要重新创建fire : 立即执行, 不会等待 interval// 当用户手指开始拖拽的时候, 就让计时器无效- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView// 当用户手指停止拖拽的时候, 重新创建计时器- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate项目一:添加随机颜色**********头文件部分************#import "ViewController.h"#define kColumn 5#define kWidth [UIScreen mainScreen].bounds.size.width#define kButtonWidth kWidth/kColumn#define random(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]#define randomColor random(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))**********声明部分************@interface ViewController ()@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;//添加按钮@property (nonatomic,strong) UIButton *button;//随机颜色数组@property (nonatomic,strong) NSMutableArray *dataArray;@end**********实现部分************#pragma mark - 加载完视图后,界面现实的东西1. 什么是UIScrollView?可滚动的view2. UIScrollView为什么要滚动? 滚动到哪里?一个屏幕不能完整的显示内容显示的内容有多大, 就滚动到内容的边界3. 什么在滚动?scrollView 的内容(content) , 要添加到scrollView上3.1 怎么添加scrollView?1> 拖放UIScrollView控件到控制器view上2> 添加scrollView的content(子view)3> 设置contentSize4. UIScrollView常用属性contentSize : 必须要大与scrollView的宽高contentInset: 内边距, 距离scrollView边框的一个距离, 如果拖动之后, content会停在设置边距的那个位置contentOffset: 偏移量, content滚动到的位置bounce : 弹簧效果, 默认开启 , 如果设置为NO, 那么将不会有弹簧的效果// 即使不设置 contentSize , 也会有弹簧效果, 但是 bounce 属性不能设置为NOalwaysBounceHorizontalalwaysBounceVertical// 水平和垂直方向上的指示器showsHorizontalScrollIndicatorshowsVerticalScrollIndicator5. ScrollView的size和contentSize的区别?contentSize : 内容的可滚动范围size: scrollView本身的 大小6.1 无法滚动的原因1. contentSize 设置的比 scrollView的size 小的时候,比可以滚动2. userInteractionEnabled = NO; 的时候, 用户不能交互3. scrollEnabled 设置为NO的时候, 将不能滚动7. 图片轮播器和pageControllUIScrollView 和 UIPageControl// pageEnabled 分页效果 , 依据scrollView的宽度来分页_scrollView.pagingEnabled = YES;8.UIPageControlnumberOfPage 有多少页currentPage 当前在第几页// 设置页码指示器的颜色_pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];// 设置当前页码指示器的颜色_pageControl.currentPageIndicatorTintColor = [UIColor redColor];scrollView 在和 pageControl 结合时候的时候- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {// 计算当前是第几张图片NSInteger currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;// 设置 pagecontorl的 当前页码_pageControl.currentPage = currentPage;}12. 自动播放NSTimer 计时器_timer = [NSTimer scheduledTimerWithTimeInterval:1 // 间隔target:self // 控制器selector:@selector(changePage) // 调用的方法userInfo:nil // 传递的参数, 如果上面方法需要参数, 就在这里传递repeats:YES]; // 是否重复timer 创建完成之后, 就直接生效invalidate 使计时器无效, 再启用计时器就需要重新创建fire : 立即执行, 不会等待 interval// 当用户手指开始拖拽的时候, 就让计时器无效- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView// 当用户手指停止拖拽的时候, 重新创建计时器- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate#pragma mark - 设置UI界面-(void)setupUI{    _button = [[UIButton alloc] initWithFrame:CGRectMake(0, 22, kButtonWidth, kButtonWidth)];    [_button setTitle:@"添加" forState:UIControlStateNormal];        [_button setBackgroundColor:[UIColor blueColor]];        _button.tag = 10;        //添加监听方法        [_button addTarget:self                action:@selector(didClickBuuton)      forControlEvents:UIControlEventTouchUpInside];    [_scrollView addSubview:_button];        [_dataArray addObject:_button];    }#pragma mark - 监听添加按钮点击事件-(void)didClickBuuton{    //实例化一个button对象    UIButton *button = [[UIButton alloc] init];        //设置背景颜色    [button setBackgroundColor:randomColor];    //添加按钮到数组    [_dataArray addObject:button];        //交换数组元素    [_dataArray exchangeObjectAtIndex:_dataArray.count-1 withObjectAtIndex:_dataArray.count-2];        //遍历数组    [_dataArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        UIButton *arrayButton = obj;    //设置当前行    NSInteger rowIndex = idx / kColumn;    //设置当前列    NSInteger columnIndex = idx % kColumn;        CGFloat startX = columnIndex*kButtonWidth;        CGFloat startY = rowIndex*kButtonWidth;    //让view以动画显示展现    [UIView animateWithDuration:0.5 animations:^{            [arrayButton setFrame:CGRectMake(startX, startY + 22, kButtonWidth, kButtonWidth)];            }];        [_scrollView addSubview:arrayButton];    }];    //将最后一个button设置为数组的最后一个元素    //数组具有最后一个元素属性    UIButton *lastButton = _dataArray.lastObject;    if (lastButton.frame.origin.y >= _scrollView.frame.size.height) {                CGFloat contentHeight = CGRectGetMaxY(lastButton.frame);                _scrollView.contentSize = CGSizeMake(0, contentHeight);                CGFloat offsetY = contentHeight - _scrollView.frame.size.height;                [_scrollView setContentOffset:CGPointMake(0, offsetY) animated:YES];            } }@end</span>



0 0
原创粉丝点击