zoukankan      html  css  js  c++  java
  • 自定义控件(3)

    功能:

    1,滚动的广告横幅


    源码地址:

     https://github.com/SSBun/iOS-Custom-Control/tree/master/滚动横幅(3)

    //
    //  BZScrollBar.h
    //  BZScrollBar
    //
    //  Created by 蔡士林 on 16/1/22.
    //  Copyright © 2016年 BZ. All rights reserved.
    //
    #import <UIKit/UIKit.h>
    
    @interface BZScrollBar : UIView
    
    @property (strong, nonatomic) NSArray *datas;
    
    @end
    //
    //  BZScrollBar.m
    //  BZScrollBar
    //
    //  Created by 蔡士林 on 16/1/22.
    //  Copyright © 2016年 BZ. All rights reserved.
    //
    
    #import "BZScrollBar.h"
    
    @interface BZScrollBar ()
    @property (strong, nonatomic) UIScrollView *scrollView;
    @property (strong, nonatomic) NSMutableArray *imageViewArr;
    @end
    
    @implementation BZScrollBar
    
    - (NSMutableArray *)imageViewArr
    {
        if (!_imageViewArr) {
            _imageViewArr = [NSMutableArray array];
        }
        return _imageViewArr;
    }
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setUpUI];
        }
        return self;
    }
    
    - (void)setUpUI
    {
        UIScrollView *scrollView = [[UIScrollView alloc]init];
        self.scrollView = scrollView;
        scrollView.pagingEnabled = YES;
        [self addSubview:scrollView];
    }
    
    - (void)setDatas:(NSArray *)datas
    {
        _datas = datas;
        for (int i = 0; i < datas.count + 2; i ++) {
            
            UILabel *label = [[UILabel alloc]init];
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [self randomColor];
            [self.scrollView addSubview:label];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:15];
            [self.imageViewArr addObject:label];
            
            if (i == 0) {
                label.text = datas[datas.count - 1];
            }
            else if (i == datas.count + 1)
            {
                label.text = datas[0];
                label.backgroundColor = ((UILabel *)self.imageViewArr[1]).backgroundColor;
            }
            else if (i == datas.count)
            {
                label.text = datas[i - 1];
                label.backgroundColor = ((UILabel *)self.imageViewArr[0]).backgroundColor;
            }
            else
            {
                label.text = datas[i - 1];
            }
        }
        [self setNeedsLayout];
        [self layoutIfNeeded];
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollBar) userInfo:nil repeats:YES];
    }
    
    - (UIColor *)randomColor
    {
        CGFloat red   = ((arc4random() % 255) + 1) / 255.0;
        CGFloat green = ((arc4random() % 255) + 1) / 255.0;
        CGFloat blue  = ((arc4random() % 255) + 1) / 255.0;
        return [UIColor colorWithRed:red green:green blue:blue alpha:1];
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        self.scrollView.frame = self.bounds;
        self.scrollView.contentOffset = CGPointMake(0, self.bounds.size.height);
        CGFloat W = self.bounds.size.width;
        CGFloat H = self.bounds.size.height;
        self.scrollView.contentSize = CGSizeMake(1, self.imageViewArr.count * self.bounds.size.height);
        for (int i = 0 ; i < self.imageViewArr.count; i ++) {
            UILabel *label = self.imageViewArr[i];
            label.frame = CGRectMake(0, H * i, W, H);
        }
    }
    
    - (void)scrollBar
    {
        if (self.scrollView.contentOffset.y >= self.scrollView.contentSize.height - self.scrollView.bounds.size.height) {
            self.scrollView.contentOffset = CGPointMake(0, self.scrollView.bounds.size.height);
        }
        CGPoint point = self.scrollView.contentOffset;
        point.y = point.y + self.scrollView.bounds.size.height;
        [UIView animateWithDuration:1 animations:^{
            self.scrollView.contentOffset = point;
        }];
    }
    
    @end

  • 相关阅读:
    URLOS用户福利:申请免费的泛域名(通配符域名)SSL证书
    主机管理面板LuManager以Apache2协议开源发布,可用作商业用途
    微服务设计概览
    使用SpringBoot搭建Web项目
    公共方法整合(四)数组相关
    阿里云短信整合封装类库
    高德地图接口使用整理
    公共方法整合(三)时间相关方法
    PHP 服务端友盟推送
    html 录音并上传
  • 原文地址:https://www.cnblogs.com/SSBun/p/5155517.html
Copyright © 2011-2022 走看看