利用UIScrollView实现几个页面的切换

此实例可以了解一下UIScrollView的运用,以及表格跟页面跳转的内容;

原作者地址:http://www.cocoachina.com/bbs/read.php?tid=323514

效果图如下:

1:知识点滚动视图的运用

#import "YCView.h"

@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)UIScrollView *scrollV;
@property (weak, nonatomic) IBOutlet UIButton *usesbtn;
@property (weak, nonatomic) IBOutlet UIButton *partBtn;
@property (weak, nonatomic) IBOutlet UIButton *serverBtn;

@end

@implementation ViewController
//懒加载
- (UIScrollView *)scrollV
{
    if(!_scrollV)
    {
        _scrollV = [[UIScrollView alloc] init];
        //设置scrollView的frame
        CGFloat scrollX = 0;
        CGFloat scrollY = 110;
        CGFloat scrollW = CGRectGetWidth(self.view.bounds);
        CGFloat scrollH = CGRectGetHeight(self.view.bounds);
        _scrollV.frame = CGRectMake(scrollX, scrollY, scrollW, scrollH);
        //设置代理
        _scrollV.delegate = self;
        //将scrollView添加到控制器的view上
        [self.view addSubview:_scrollV];

    }
    return _scrollV;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //添加视图 view
    [self addScrollView];
    self.scrollV.contentOffset = CGPointMake(0, 0);
}

- (void)addScrollView
{
    //添加3个view
    for(int i = 0; i < 3; i++)
    {
       CGFloat viewX = i * [UIScreen mainScreen].bounds.size.width;
       CGFloat viewY = 0;
       CGFloat viewW = [UIScreen mainScreen].bounds.size.width;
       CGFloat viewH = [UIScreen mainScreen].bounds.size.height - 108;
        YCView *v = [[YCView alloc] initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)];
        v.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/ 255.0 green:arc4random_uniform(255)/ 255.0 blue:arc4random_uniform(255)/ 255.0 alpha:1.0];
        [self.scrollV addSubview:v];
    }
    //设置frame,偏移量
    //设置分页
    self.scrollV.pagingEnabled = YES;
    self.scrollV.backgroundColor = [UIColor orangeColor];
    //设置滚动范围
    self.scrollV.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 3, [UIScreen mainScreen].bounds.size.height);
    //设置偏移量
    self.scrollV.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
    //取消scrollView滚动到边缘的弹簧效果
    self.scrollV.bounces = NO;
    //隐藏水平滚动条
    self.scrollV.showsHorizontalScrollIndicator = NO;
}

#pragma mark --UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //设置按钮被选中状态下的颜色
    scrollView.contentOffset.x == 0 ? [self.usesbtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.usesbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    scrollView.contentOffset.x == ([UIScreen mainScreen].bounds.size.width) ? [self.partBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.partBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    scrollView.contentOffset.x == ([UIScreen mainScreen].bounds.size.width) * 2 ? [self.serverBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal] : [self.serverBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

#pragma mark --btnClick
- (IBAction)usesBtnClick:(id)sender {
    //跳转到第1个view  contentOffset.x = 屏幕的宽度 * 0
    //重置scrollView的位置
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:0];
    }];
}

- (IBAction)partBtnClick:(id)sender {
    //跳转到第2个view  contentOffset.x = 屏幕的宽度 * 1
    //重置scrollView的位置
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:1];
    }];

}

- (IBAction)serverBtnClick:(id)sender {
    //跳转到第3个view  contentOffset.x = 屏幕的宽度 * 2
    //重置scrollView的位置
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollV.contentOffset = [self ScrollViewWithContentOffSetPage:2];
    }];
}

//返回scrollView偏移量
- (CGPoint)ScrollViewWithContentOffSetPage:(NSInteger)page{
    return CGPointMake(([UIScreen mainScreen].bounds.size.width) * page, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2:列表及跳转跳显示的内容

#import "YCView.h"
#import "YCCellView.h"

static NSString *idenifer = @"YCCollectionViewCell";
#define CellWeigth ([UIScreen mainScreen].bounds.size.width)/3.0
#define CellHeigth 44
@interface YCView ()<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic)NSArray *parts;
@property (strong, nonatomic)NSMutableArray *Views;
@end

@implementation YCView
//懒加载
- (NSMutableArray *)Views{
    if(!_Views){
        _Views = [NSMutableArray array];
    }
    return _Views;
}
//懒加载
- (NSArray *)parts{
    if(!_parts)
    {
        _parts = [NSArray array];
        _parts = @[@"热门推荐", @"汽车外饰", @"香水/净化", @"功能用品", @"美容养护", @"安全/防护", @"影音导航"];
    }
    return _parts;
}

- (instancetype)init
{
    if(self = [super init])
    {
        [self addView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
         [self addView];
    }
    return self;
}
- (void)addView
{
    //添加tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -44, CellWeigth, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor redColor];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self addSubview:tableView];
}
#pragma mark --UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.parts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"YCCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = self.parts[indexPath.row];
    return cell;
}

#pragma mark --UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self addBestView];
}

- (void)addBestView{
    YCCellView *view = [[YCCellView alloc] initWithFrame:CGRectMake(CellWeigth, 0, ([UIScreen mainScreen].bounds.size.width)-CellWeigth, [UIScreen mainScreen].bounds.size.height)];
    view.backgroundColor = [UIColor redColor];
    [[self.Views lastObject] removeFromSuperview];
    [self.Views addObject:view];
    [self addSubview:view];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}

@end

3:单元列的内容

#import "YCCellView.h"

#define ViewMagin 10
#define ViewHeight 90
#define ViewWeight (([UIScreen mainScreen].bounds.size.width)-CellWeigth - 3*ViewMagin)/3.0
#define CellWeigth ([UIScreen mainScreen].bounds.size.width)/3.0
@interface YCCellView ()
@end
@implementation YCCellView
- (instancetype)init
{
    if(self = [super init])
    {
        [self addCollectionView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self addCollectionView];
    }
    return self;
}

- (void)addCollectionView
{
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            UIView *v = [[UIView alloc] initWithFrame:CGRectMake(j * (ViewWeight + ViewMagin), i * (ViewHeight + ViewMagin), ViewWeight, ViewHeight)];
            v.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/ 255.0 green:arc4random_uniform(255)/ 255.0 blue:arc4random_uniform(255)/ 255.0 alpha:1.0];
            [self addSubview:v];
        }
    }
}

@end
时间: 2024-08-03 14:34:32

利用UIScrollView实现几个页面的切换的相关文章

利用pushState开发无刷页面切换

利用pushState开发无刷页面切换<转> 相关文档:https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulatingthebrowser_history 实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与页面展现内容一致(符合人们对传统网页的认识) 在不支持的浏览器下降级成传统网页的方式 使用到的API history.state 当前URL下对应的状态信息.如果当前URL不是通过pushSta

ViewPager(下)--利用Fragment实现漂亮的页面切换

之前用的ViewPager适用于简单的广告切换,但实现页面间的切换最好是用官方推荐的Fragment来处理. 本人力争做到最简单.最实用,是想以后用到的时候可以方便的拿过来复制就可以了. 效果图:          功能:可点击按钮切换页面,也可滑动界面切换页面. 文件:java文件5个.xml文件4个..9图片4张 java主类: <span style="font-size:12px;">package com.example.viewpageractivity; im

iOS 自定义页面的切换动画与交互动画

在iOS7之前,开发者为了寻求自定义Navigation Controller的Push/Pop动画,只能受限于子类化一个UINavigationController,或是用自定义的动画去覆盖它.但是随着iOS7的到来,Apple针对开发者推出了新的工具,以更灵活地方式管理UIViewController切换. 自定义导航栏的Push/Pop动画 为了在基于UINavigationController下做自定义的动画切换,先建立一个简单的工程,这个工程的rootViewController是一个

ViewPager页面滑动切换

我们日常生活中用到的微博,QQ,微信等app在进行页面左右滑动的时候,很多都可以用ViewPager来实现.可以说,ViewPager在android开发中十分常见并且特别实用. Viewpager在android.support.v4.view这个软件包中,  android.support.v4.view.jar是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.所以低版本开发时必须加入android-support-v4.jar,并且在XM

JSP中如何利用JS实现登录页面的跳转(JSP中如何利用JS实现跳转页面)

JSP中如何利用JS实现登录页面的跳转(JSP中如何利用JS实现跳转页面) 注:只是用到js中的setTimeout();具体使用方法:  setTimeout(函数名, 时间(毫秒)); setTimeout( ) 是属于 window 的 method, 但我们都是略去 window 这顶层物件名称, 这是用来设定一个时间, 时间到了, 就会执行一个指定的 method.setTimeout( ) 通常是与 function一起用. 样例: 1 <%@ page language="j

Android--ViewPager多页面滑动切换以及动画效果

背景                                                                                           ViewPager.它是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换.这个附加包是android-support-v4.jar. 根据屏幕的分辨率和图片的宽度计算动画移动的偏移量 代码                                                   

怎么实现在jenkens页面配置sonar,以及如何利用cobertura插件实现sonar页面显示测试覆盖率

最近我在工作中遇到需要给项目中每个模块配置Sonarqube,来看看测试用例覆盖的情况.在这个过程中遇到了一些问题,也查了很多资料.现在记录一下具体应该怎么配置. 先展示一下实现的效果: 图一 It will show: 图二 现在来看看具体配置: 要enable Sonarqube, 需在 job --> configure --> Post-build Actions 配置Branch 和 JDK 信息: 图三 配置好了以后,SonarQube 可以在页面上显示出来了,点击进去,效果图:[

ViewPager+Fragment实现页面的切换

新知识,新摘要: 效果图:framgent导入包都是v4包下,谨慎导入错误! 首先设置viewPager布局: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" and

android(8) ViewPager页面滑动切换

一.ViewPager页面滑动切换实现:    借鉴了别人的源码,还是比较容易实现的,而且这种效果还是经常使用的,特此记录一下: 效果图: 主界面: public class MainActivity extends Activity { private ViewPager mPager;// 页卡内容 private List<View> listViews; // Tab页面列表 private ImageView cursor;// 动画图片 private TextView t1, t