轮播图 - 拖拽交互关系

  现如今新闻类的APP一般都有轮播图,最常见的设计就是给轮播图一个计时器,让其自己每隔几秒轮播到下一张。但如果用户自行拖拽时计时器就应该失去响应,以用户的手势方法为第一响应;当用户停止手势触发轮播图时,计时器又会开始生效。
  新建一个project后,一如既往的先在Delegate.m中设置窗口:
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]   ;
  _window.backgroundColor = [UIColor whiteColor];
  _window.rootViewController = [[ViewController alloc] init];
  [_window makeKeyAndVisible];
  接着,我们要在ViewController里,设置我们需要的工具:
   UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
   scrollView.backgroundColor = [UIColor colorWithRed:0.722 green:1.000       blue:0.817 alpha:1.000];
   [self.view addSubview:scrollView];
  并开始设置scrollView的一些变量
1.内容的范围 VGSizeZero  CGSizeMake(0, 0)
 contentSize 设置可以滚动的范围
scrollView.contentSize = CGSizeMake(0, 1000);
 contentOffset 偏移量,滚动的范围
 不设置contentSize,contentOffset也可以改变
 那么改变:contentOffset什么变了?
  其实 改变contentOffset, 实质上是对scrollView的Bounds进行修改
NSLog(@"改变前frame:%@",NSStringFromCGRect(scrollView.frame));
NSLog(@"改变前bounds:%@",NSStringFromCGRect(scrollView.bounds));
scrollView.contentOffset
= CGPointMake(0, 500);
NSLog(@"改变后frame:%@",NSStringFromCGRect(scrollView.frame));
NSLog(@"改变后bounds:%@",NSStringFromCGRect(scrollView.bounds));

 3.contentIset
 添加滚动区域四周的滚动范围
scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);

 4.directionalLockEnabled
 锁定垂直或水平滚动(45°角, 锁会失效)
scrollView.directionalLockEnabled = YES;

 5. bounces 回弹效果
    scrollView.bounces = NO;

 6.alawaysBounceVertical
在没有垂直方向的滚动范围时,保证垂直方向的垂直效果存在
scrollView.alwaysBounceVertical = YES;

 7,alwaysBounceHorizontal
在没有水平方向的滚动范围时,保证水平方向的垂直效果存在
scrollView.alwaysBounceHorizontal = YES;

 8.pagingEnabled 按页滚动
scrollView.pagingEnabled = YES;

 9 视图时候能够滚动,默认是YES
scrollView.scrollEnabled = YES;

 10.指示条 默认是YES
scrollView.showsVerticalScrollIndicator = YES;
scrollView.showsHorizontalScrollIndicator = YES;

 11. 滚动指示器的边距
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);

 12.滚动指示器的颜色,背景,深色,浅色,通用
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;

13.
 tracking 用户是否开始拖动
    scrollView.tracking
dragging 用户时候正在拖动
    scrollView.dragging
decelerating 用户时候在减速中
    scrollView.decelerating

14.滚动视图,双击状态栏是否存在 默认 YES
scrollView.scrollsToTop = YES;

15.scrollView 的代理
scrollView.delegate = self;

16.键盘消失模式
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;



我们来测试一下 先设置一个TextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 350, WIDTH - 20, 200)];
textField.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:textField];

17.缩放系数
如果想进行视图缩放,必须实现viewForZoomingInScrollView:
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2;


再来设置一些方法

与其想要放大的子视图绑定,scrollView的contentSize会跟随这个子视图变化

  • (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
    }
    滚动视图滚动,就会执行

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"滚动中");

    Trackong 用户触摸
    if (scrollView.tracking) {
    NSLog(@"正在拖拽滚动");

    } else {
    NSLog(@"自己滚动");
    }
    Dragging 用户开始滑动
    if (scrollView.dragging) {
    NSLog(@"");
    }
    Decelerating 用户触摸结束
    if (scrollView.decelerating) {
    NSLog(@"");
    }
    }

将要开始拖拽

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"开始拖拽");

}

将要结束拖拽

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

}

已经结束拖拽

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"已经结束拖拽");

}

  • (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

}

以上就是轮播图的大概概念,下面分享一下我做的简易的轮播图:
//
// AppDelegate.m
// 05- HomeWork
//
// Created by 李昊林 on 16/7/20.
// Copyright © 2016年 李昊林. All rights reserved.
//

import "AppDelegate.h"

import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    _window.rootViewController = [[ViewController alloc] init];
    [_window makeKeyAndVisible];

    return YES;
    }

  • (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

  • (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end

//
// ViewController.m
// 05 - HomeWork
//
// Created by 李昊林 on 16/7/20.
// Copyright © 2016年 李昊林. All rights reserved.
//

import "ViewController.h"

import "AppDelegate.h"

define FRAME [UIScreen mainScreen].bounds

define WIDTH FRAME.size.width

define HEIGHT FRAME.size.height

@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,retain) UIScrollView *scrollViewOfAlbum;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    [self makeAlbum];
    [self addPage];
    // Do any additional setup after loading the view, typically from a nib.
    // 完善ScrollView相册, 实现轮播及缩放恢复。
    // 封装轮播图
    // 添加UIPageControl, 绑定事件, 有点击切换效果
    // 不用添加定时器

}

  • (void)makeAlbum {
    UIScrollView *album = [[UIScrollView alloc] initWithFrame:FRAME];
    [self.view addSubview:album];
    NSArray *arr = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", nil];
    album.contentSize = CGSizeMake(WIDTH * arr.count, HEIGHT);
    album.pagingEnabled = YES;
    album.bounces = NO;
    album.delegate = self;
    for (NSInteger i = 0; i < arr.count ; i++) {
    UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    smallScrollView.delegate = self;
    img.image = [UIImage imageNamed:arr[i]];
    [smallScrollView addSubview:img];
    [album addSubview:smallScrollView];
    smallScrollView.tag = i;
    smallScrollView.minimumZoomScale = 0.5;
    smallScrollView.maximumZoomScale = 2;
    }
    album.contentOffset = CGPointMake(WIDTH, 0);
    album.bounces = NO;
    album.showsHorizontalScrollIndicator = NO;
    }
  • (void)addPage{
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
    self.page.currentPage = 0;
    self.page.numberOfPages = 3;
    [self.view addSubview:self.page];
    }
  • (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    _index = scrollView.tag;
    return [scrollView.subviews firstObject];
    }
  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.page.currentPage = index - 1;
    if (index == 3) {
    self.page.currentPage = 0;
    scrollView.contentOffset = CGPointMake(0, 0);
    }
    if (index == 0) {
    self.page.currentPage = 3;
    scrollView.contentOffset = CGPointMake(WIDTH * 3, 0);
    }
    UIScrollView *scroll = [scrollView.subviews objectAtIndex:_index];
    scroll.zoomScale = 1;
    }
  • (void)pageAction:(UIPageControl *)page{
    // 点击换图
    [self.scrollViewOfAlbum setContentOffset:CGPointMake(WIDTH * (self.page.currentPage), 0) animated:YES];
    }
  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end
这就是我做的建议轮播图,比较简单,但是确实可以做到轮转效果。好了 祝大家有个愉快的周末。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容