当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS多控制器实现带滑动动画第1/2页

iOS多控制器实现带滑动动画第1/2页

2019年07月24日  | 移动技术网IT编程  | 我要评论

乌鲁木齐二手房出售,qq头像发布中心个性网,qq空间相册封面拼图

本文实例为大家分享了ios多控制器实现带滑动动画的具体代码,供大家参考,具体内容如下

主控制器 ,管理控制器 .h文件

//宏
#define kscreenwidth [uiscreen mainscreen].bounds.size.width
#define kscreenheight [uiscreen mainscreen].bounds.size.height

#import "mymainviewcontroller.h"
#import "myfirstviewcontroller.h"
#import "mysecondviewcontroller.h"
#import "mythirdviewcontroller.h"

@interface mymainviewcontroller ()<uiscrollviewdelegate>
//控制器名
@property (nonatomic, strong) nsarray *vcnames;
//选择栏
@property(nonatomic, strong) uiview *clickbar;
//底部容器scrollview
@property (strong, nonatomic) uiscrollview *containerscrollerview;

@end

. m 文件

底部scrollview , 用于滑动

@implementation mymainviewcontroller

- (uiscrollview *)containerscrollerview
{
  if (!_containerscrollerview) {
    _containerscrollerview = [[uiscrollview alloc]init];
    _containerscrollerview.pagingenabled = yes;
    _containerscrollerview.showsverticalscrollindicator = no;
    _containerscrollerview.showshorizontalscrollindicator = no;
    _containerscrollerview.contentsize = cgsizemake(kscreenwidth *self.vcnames.count,kscreenheight);
    _containerscrollerview.backgroundcolor = [uicolor whitecolor];
    _containerscrollerview.delegate = self;
  }
  return _containerscrollerview;
}

初始化顶部选择栏

//三个子控制器
- (nsarray *)vcnames
{
  if (!_vcnames) {
    _vcnames = @[@"控制器一",@"控制器二",@"控制器三"];
  }
  return _vcnames;
}

//点击选择栏
- (uiview *)clickbar
{
  if (!_clickbar) {
    _clickbar = [[uiview alloc]init];
    _clickbar.backgroundcolor = [uicolor lightgraycolor];

    cgfloat width = kscreenwidth / 3;
    cgfloat height = 44;
    //初始化按钮
    for (nsinteger index = 0; index < 3; index++) {

      uibutton *button = [uibutton buttonwithtype:uibuttontypecustom];
      [button settitle:self.vcnames[index] forstate:uicontrolstatenormal];
      [button settitlecolor:[uicolor bluecolor] forstate:uicontrolstatenormal];
      button.frame = (cgrect){width *index,0,width,height};
      [button addtarget:self action:@selector(buttonclick:) forcontrolevents:uicontroleventtouchupinside];

      //绑定tag值
      button.tag = index;
      [_clickbar addsubview:button];
    }
  }
  return _clickbar;
}

viewdidload

- (void)viewdidload {
  [super viewdidload];

  self.edgesforextendedlayout = 0;
  //初始化选择栏
  [self initclickbar];

  //初始化底部scrollview容器
  [self initscrollviewcontainer];

  //初始化子控制器
  [self addchildcontrollers];

}

添加子控制器 , 初始化ui

//按钮选择栏
- (void)initclickbar
{
  [self.view addsubview:self.clickbar];
  self.clickbar.frame = (cgrect){0,0,[uiscreen mainscreen].bounds.size.width,44};
}

//初始化滑动容器
- (void)initscrollviewcontainer
{
  [self.view addsubview:self.containerscrollerview];
  self.containerscrollerview.frame = cgrectmake(0,44,kscreenwidth, kscreenheight );
}

//添加子控制器
- (void)addchildcontrollers
{
  //为了方便直观 , 在此处设置背景色 (实际开发中,不能在这里设置 , 原因是这里只要调用到了控制器的view属性 , 该控制器将会执行viewdidload方法 , 相当于直接一开始就将三个控制器的所有ui和网络请求全加载完了 , 负荷会相当重)
  myfirstviewcontroller *firstvc = [[myfirstviewcontroller alloc]init];
  firstvc.view.backgroundcolor = [uicolor redcolor];
  [self addchildviewcontroller:firstvc];

  mysecondviewcontroller *secondvc = [[mysecondviewcontroller alloc]init];
  secondvc.view.backgroundcolor = [uicolor bluecolor];
  [self addchildviewcontroller:secondvc];

  mythirdviewcontroller *thirdvc = [[mythirdviewcontroller alloc]init];
  thirdvc.view.backgroundcolor = [uicolor yellowcolor];
  [self addchildviewcontroller:thirdvc];

  //默认展示第一个子控制器
  [self scrollviewdidenddecelerating:self.containerscrollerview];
}

按钮点击事件实现 , 代理方法实现

//选择栏按钮点击事件
- (void)buttonclick:(uibutton *)button
{
  [self.containerscrollerview setcontentoffset:cgpointmake(button.tag *kscreenwidth, 0) animated:yes];
}

//滑动减速时调用
- (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview
{
  //获取contentoffset
  cgpoint currentoffset = scrollview.contentoffset;

  nsinteger page = currentoffset.x / kscreenwidth;

  //取出对应控制器
  uiviewcontroller *viewcontroller = self.childviewcontrollers
            12下一页阅读全文
            
您可能感兴趣的文章:ios使用pageviewcontroller实现多视图滑动切换ios左右滑动标签页导航的设计ios仿今日头条滑动导航栏ios滑动解锁、滑动获取验证码效果的实现代码ios scrollview嵌套tableview同向滑动的示例微信浏览器弹出框滑动时页面跟着滑动的实现代码(兼容android和ios端)ios开发中禁止navigationcontroller的向右滑动返回ios开发向右滑动返回前一个页面功能(demo)ios实现双向滑动条效果uipageviewcontroller实现的左右滑动界面




        

相关文章

  • ios实现uitableview数据为空时的提示页面

    ios实现uitableview数据为空时的提示页面

    最近工作中遇到一个需求,当uitableview数据为空的时候,给出一个简单的提示页面,通过从网上查找解决的方法,发现了两种实现的方法,现在分享给大家,有需要的朋友们可以参考借鉴,下面感兴趣的朋友们来一起学习学习吧。
    2016-11-11
  • ios计算上次日期距离现在多久的代码

    ios计算上次日期距离现在多久的代码

    这篇文章主要为大家详细介绍了ios计算上次日期距离现在多久的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • ios使用xib手动实现动画效果的方法

    ios使用xib手动实现动画效果的方法

    下面小编就为大家分享一篇ios使用xib手动实现动画效果的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • ios开发中一些手写控件及其相关属性的使用

    ios开发中一些手写控件及其相关属性的使用

    这篇文章主要介绍了ios开发中一些手写控件及其相关属性的使用,代码基于传统的objective-c,需要的朋友可以参考下
    2015-12-12
  • ios中 uiimage根据屏宽调整size的实例代码

    ios中 uiimage根据屏宽调整size的实例代码

    最近做项目遇到这样一个需求,要求uiimage根据屏幕宽度按照自己本身比例改变高度,下面通过本文给大家分享ios uiimage根据屏宽调整size的实例代码,需要的朋友参考下吧
    2017-01-01
  • ios11.3以下modal中input光标错位的解决方法

    ios11.3以下modal中input光标错位的解决方法

    这篇文章主要介绍了ios11.3以下modal中input光标错位的解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • ios中实现检测zoombie对象的具体方法

    ios中实现检测zoombie对象的具体方法

    这篇文章主要给大家介绍了关于ios中实现检测zoombie对象的具体方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-01-01
  • ios身份证号码识别示例

    ios身份证号码识别示例

    本篇文章主要介绍了ios身份证号码识别示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • 举例讲解ios应用开发中对设计模式中的策略模式的使用

    举例讲解ios应用开发中对设计模式中的策略模式的使用

    这篇文章主要介绍了ios应用设计中对设计模式中的策略模式的使用,示例代码为传统的objective-c语言,需要的朋友可以参考下
    2016-03-03
  • ios实现的多条折线图封装实例

    ios实现的多条折线图封装实例

    这篇文章主要跟大家分享了关于利用ios实现多条折线图的封装实例,文中给出了详细的示例代码供大家参考学习,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-07-07

最新评论

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网