美文网首页
scrollView不分页滑动

scrollView不分页滑动

作者: Dove_Q | 来源:发表于2016-10-17 09:31 被阅读43次
#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    _scrollView.backgroundColor = [UIColor greenColor];
    _scrollView.contentSize = CGSizeMake(self.view.bounds.size.width*20, 0);
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    for (int i = 1; i<=20; i++) {
        UIView *v = [UIView new];
        v.frame = CGRectMake(i*self.view.bounds.size.width, 0, self.view.bounds.size.width, 200);
        v.backgroundColor = [self randomColor];
        [_scrollView addSubview:v];
    }
}
//产生随机颜色
- (UIColor*)randomColor {
    float red_value = arc4random() % 255 + 1;
    float green_value = arc4random() % 255 + 1;
    float blue_value = arc4random() % 255 + 1;
    UIColor *color = [UIColor colorWithRed:red_value/255.0 green:green_value/255.0 blue:blue_value/255.0 alpha:1];
    return color;
}
//方法很重要
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    
    //四舍五入
    NSInteger count = round(targetContentOffset->x / scrollView.bounds.size.width);
    targetContentOffset->x = count * scrollView.bounds.size.width;
    //输入: 2.8----->输出: 2
    NSLog(@"%lf",floor(2.8)) ;
    //输入: 2.3----->输出: 3
    NSLog(@"%lf",ceil(2.3));
    
}

@end

相关文章

网友评论

      本文标题:scrollView不分页滑动

      本文链接:https://www.haomeiwen.com/subject/knmayttx.html