美文网首页
2020-09-10

2020-09-10

作者: 驰马奥 | 来源:发表于2020-09-10 01:06 被阅读0次

随笔

scrollview的应用

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic,assign) CGFloat itemWidth;

@property (nonatomic,assign) CGFloat offset;

@property (nonatomic,assign) CGFloat screenWidth;

@property (nonatomic,assign) CGFloat contentSizeWidth;

@property (nonatomic,assign) CGFloat position;

@end

- (void)viewDidLoad {

    [super viewDidLoad];

    CGFloatscreenWidth = [UIScreenmainScreen].bounds.size.width;

    self.screenWidth= screenWidth;

    CGFloatscrollX =0;

    CGFloatscrollW = screenWidth -2* scrollX;

    UIScrollView*scrollview = [[UIScrollViewalloc]initWithFrame:CGRectMake(scrollX ,100, scrollW,50)];

    scrollview.delegate=self;

    scrollview.bounces=NO;

    scrollview.showsVerticalScrollIndicator = NO;

    scrollview.showsHorizontalScrollIndicator = NO;

    scrollview.backgroundColor = [UIColor redColor];

    [self.viewaddSubview:scrollview];

    CGFloattargetViewX =0;

    CGFloattargetViewY =0;

    CGFloattargetViewW =300;

    CGFloattargetViewH =50;

    UIView*targetView = [[UIViewalloc]initWithFrame:CGRectMake(targetViewX, targetViewY, targetViewW, targetViewH)];

    targetView.backgroundColor = UIColor.blueColor;

    [scrollviewaddSubview:targetView];

    self.itemWidth= targetViewW;

    UIView*lineView = [[UIViewalloc]initWithFrame:CGRectMake(screenWidth/2.0-1,95,2,60)];

    lineView.backgroundColor = [UIColor greenColor];

    [self.viewaddSubview:lineView];

    CGFloatcontentSizeWidth =2* targetViewW + scrollW;

    self.contentSizeWidth= contentSizeWidth;

    scrollview.contentSize=CGSizeMake(contentSizeWidth, targetViewH);

    scrollview.contentOffset=CGPointMake(contentSizeWidth/2.0- screenWidth/2.0,0);

    CGFloatoffSet =  800;

    if(offSet <0) {

        offSet =0;

    }elseif(offSet >  self.itemWidth){

        offSet =self.itemWidth;

    }

    self.offset= offSet;

    targetViewX = contentSizeWidth/2- offSet;

    targetView.frame=CGRectMake(targetViewX, targetViewY, targetViewW, targetViewH);

    UIView*blackView = [[UIViewalloc]initWithFrame:CGRectMake(contentSizeWidth/2.0,0,2,50)];

    blackView.backgroundColor = [UIColor blackColor];

    [scrollviewaddSubview:blackView];

    UIView*lineView2 = [[UIViewalloc]initWithFrame:CGRectMake(0,25, scrollview.contentSize.width,3)];

    lineView2.backgroundColor = UIColor.whiteColor;

    NSLog(@"--scrollview.contentSize.width---%f",scrollview.contentSize.width);

    NSLog(@"--scrollview.contentSize.width---%f",scrollview.contentOffset.x);

    [scrollviewaddSubview:lineView2];

}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView{

    NSLog(@"----%lf",scrollView.contentOffset.x);

    if(scrollView.contentOffset.x<=self.itemWidth-self.offset) {

        scrollView.contentOffset=CGPointMake(self.itemWidth-self.offset,0);

    }elseif(scrollView.contentOffset.x>  ((self.contentSizeWidth-self.screenWidth)/2.0+self.itemWidth-self.offset)){

        scrollView.contentOffset=CGPointMake((self.contentSizeWidth/2.0-self.screenWidth/2.0+self.itemWidth-self.offset),0);

    }

    self.position= scrollView.contentOffset.x;

}                                           

相关文章

  • 魏城《还梦》

    还梦 作者:魏城 初稿于2016-02-25 01:45 修改于2020-09-10 09:05 ...........

  • 防止XSS脚本注入-前端vue、后端springboot

    防止XSS脚本注入-前端、后端 作者时间雨中星辰2020-09-10 xss是什么 跨站脚本攻击(XSS),是目前...

  • 安全底线

    我怎么如此幸运-重生12-戴红霞(2020-09-10) 我怎么如此幸运 1.我怎么如此幸运刚才听了最新的啾啾与门...

  • 2020-09-10 “骗”吃的教师节 三宝妈成长日记第52篇

    2020-09-10 星期四 阴有雨 三宝妈成长日记第52篇 三宝妈:1.早起完成财务自由课程,学习赖老师财富课程...

  • 能力培养:小学数学应将未知数更早引入求解问题当中

    2020-09-10在陪孩子求解一些奥数问题时,感觉自己很无知。一些题目是看了答案后才能理解。一些问题看了简要的答...

  • 2020-09-10

    今日体验:下午霸道保养 把客户来的主要问题先维修了 然后检查出来的问题列单子 最后把一些重要的和底盘件给维修了。针...

  • 2020-09-10

    先祝自己教师节快乐! 蒙以养正,圣功也。 昨天晚上要他爸爸把家长大学的课程打卡完,每一篇小文章看起来一分钟都不到,...

  • 2020-09-10

    今天我爸喝酒完在我面前哭了,说他舍不得我走。 我第一次见他哭得这么失态。我好难过。 和这个比起来,北京CPA取消和...

  • 2020-09-10

    今天断更快一周了,不能这样,以后需要每天都写,都发。 昨天买了招商银行3000股,今天买了,赚了200多元。 今天...

  • 2020-09-10

    头晕脑胀的一天。下午在研究劳动仲裁的事宜,那个麻烦啊,那些要求的东西一件也没,合同没有,银行流水没有,考勤表也没有...

网友评论

      本文标题:2020-09-10

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