美文网首页
滚动的广告

滚动的广告

作者: 师景福 | 来源:发表于2016-09-24 10:16 被阅读16次

////  ViewController.m//  svc////  Created by sjf on 16/9/9.//  Copyright © 2016年 sjf. All rights reserved.//#import "ViewController.h"#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)@interface ViewController (){

// UIScrollView 以及数据


UIScrollView *_scrollView;

UIPageControl *_pageControll;

NSArray *imgarr;

}

// 定时器

@property (nonatomic, strong) NSTimer *timer;

// 数量

@property (nonatomic,assign) NSUInteger count;

//位移

@property (nonatomic,assign) CGFloat oldContentOffsetX;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

_scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 193)];

[self.view addSubview:_scrollView];

_scrollView.backgroundColor =[UIColor whiteColor];

_pageControll =[[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50,_scrollView.frame.size.height-30, 100, 30)];

[self.view addSubview:_pageControll];

[self afn];

}

#pragma mark 请求UIScrollView的数据图片

-(void)afn{

// 做请求 添加数据

[self addscrollview];

}

#pragma mark 设置UIScrollView滚动条

-(void)addscrollview{

// 图片的数量  可以根据上面的 计算

self.count = 5;

// 设置控制器成为代理

_scrollView.delegate = self;

for (int i = 0; i < self.count+1; i++) {

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, 193)];

imageView.tag =50+i;

imageView.backgroundColor =[UIColor redColor];

NSString *imageName;

if (i == self.count) {

imageName = [NSString stringWithFormat:@"%d",1];

}else

{

imageName = [NSString stringWithFormat:@"%d",i + 1];

}

imageView.image = [UIImage imageNamed:imageName];

[_scrollView addSubview:imageView];

}

_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (self.count + 1), 0);

_scrollView.showsHorizontalScrollIndicator = NO;

_scrollView.pagingEnabled = YES;

_pageControll.numberOfPages = self.count;

_pageControll.currentPage = 0;

_pageControll.pageIndicatorTintColor = [UIColor orangeColor];

// 设置 当前页码指示器颜色

_pageControll.currentPageIndicatorTintColor = [UIColor redColor];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doDoubleTap:)];//默认点击第一张

//添加点击手势

[_scrollView addGestureRecognizer:doubleTap];

[self startTimer];

}

#pragma mark 手势tap

-(void)doDoubleTap:(UITapGestureRecognizer *)tap{

CATransition *animation = [CATransition animation];

//创建一个转场动画。

animation.duration = 0.8;

//动画时间

animation.subtype = kCATransitionFromTop;

//动画方向

//    animation.type = @"pageCurl";

animation.type = @"rippleEffect";

[tap.view.layer addAnimation:animation forKey:@"aaa"];

NSLog(@"%ld",_pageControll.currentPage);

}

#pragma mark 给uiscrollview添加定时器

- (void)startTimer {

_timer = [NSTimer scheduledTimerWithTimeInterval:3

target:self

selector:@selector(changeImage)

userInfo:nil

repeats:YES];

// 调整timer 的优先级

NSRunLoop *mainLoop = [NSRunLoop mainRunLoop];

[mainLoop addTimer:_timer forMode:NSRunLoopCommonModes];

}

#pragma mark 定时器的事件

- (void)changeImage {

[_scrollView setContentOffset:CGPointMake((_pageControll.currentPage+1)*SCREEN_WIDTH, 0) animated:YES];

}

- (void)stopTimer

{

[self.timer invalidate];

self.timer = nil;

}

#pragma mark uiscrollerview 的滚动方法

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

if(scrollView==_scrollView){

CGPoint point = scrollView.contentOffset;

BOOL isRight = self.oldContentOffsetX < point.x;

self.oldContentOffsetX = point.x;

// 开始显示最后一张图片的时候切换到第二个图

if (point.x > SCREEN_WIDTH*(self.count-1)+SCREEN_WIDTH*0.5 && !self.timer) {//从最后一个图片会到第一个图片

_pageControll.currentPage = 0;

}else if (point.x > SCREEN_WIDTH*(self.count-1) && self.timer && isRight){

_pageControll.currentPage = 0;

}else{

_pageControll.currentPage = (point.x + SCREEN_WIDTH*0.5) / SCREEN_WIDTH;

}

// 开始显示第一张图片的时候切换到倒数第二个图

if (point.x >= SCREEN_WIDTH*self.count) {

//kScrollViewWidth+point.x-kScrollViewWidth*(self.count+1)

[_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];

}else if (point.x < 0) {

[scrollView setContentOffset:CGPointMake(point.x+SCREEN_WIDTH*(self.count), 0) animated:NO];

}

}

}

#pragma mark 计时器停止

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

if (scrollView==_scrollView) {

[self stopTimer];

}

}

#pragma mark 手指离开屏幕 计时器开始工作

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

if(scrollView==_scrollView){

[self startTimer];

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

相关文章

  • 滚动的广告

    //// ViewController.m// svc//// Created by sjf on 16/9/9....

  • Android滚动条广告实现

    前言 几乎每个上线的App上面都会有个滚动条广告,滚动条广告主要以文字标题的形式存在,什么点开文章你就能赚一百万啊...

  • iOS 广告滚动效果

    滚动原理 之前项目有一个简单的广告滚动效果,就自己动手写了一个,比较简单,原理就是两个label的循环替换,可以向...

  • 滚动灯箱的材质选用于安装事宜

    滚动灯箱的材质选用于安装事宜 发布时间:2017-03-27 11:01 滚动灯箱 经常作为广告进行展示,由于采用...

  • 循环 ViewPager 的两种实现方法

    ViewPager 是不能循环滚动的,但是在项目中可能需要用到循环滚动的 ViewPager,比如广告栏。下面介绍...

  • 小坑

    大家在做滚动广告的时候,如果遇到第一张图片向下偏移的话,在你创建滚动视图的Viewcontroller里面加上se...

  • Flutter沉浸式Banner + 滚动修改状态栏颜色

    Flutter沉浸式Banner + 滚动修改状态栏颜色 PS:最近项目有个非人类需求,顶部放轮播广告,滚动切换状...

  • 摆脱第三方库系列(二)- 自己写一个滚动广告

    前言 摆脱第三方库系列第二章,我将介绍滚动广告的写法。在一些信息聚合类APP中滚动广告非常常见或者说必不可少,他可...

  • Android手势分发和嵌套滚动机制

    Android手势分发和嵌套滚动机制 前言 在开始介绍下面的嵌套滚动时有必要先打个广告,我们的APP可以在 Fin...

  • 循环滚动展示 label

    循环滚动展示 label业务需要做了一个上下滚动展示的文字广告位,类似轮播图的效果 Github 上的 Demo ...

网友评论

      本文标题:滚动的广告

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