• 图片滚动


     

     

     

     

          

    #import "FIrstViewController.h"

     

    @interface FIrstViewController () {

        UIScrollView *mainSV;

    }

     

    @end

     

    @implementation FIrstViewController

     

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

        }

        return self;

    }

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view from its nib.

        if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {

            self.automaticallyAdjustsScrollViewInsets = NO;

        }

        

        self.navigationItem.title = @"第1个";

        

        //上面的scrollview

        UIScrollView *upSV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 320, 150)];

        upSV.contentSize = CGSizeMake(400, 150);//设置区域

        for (int i = 0; i < 4; i++) {//把图片放进去

            UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*100, 0, 100, 150)];

            iv.userInteractionEnabled = YES;

            iv.tag = i+10;

            iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];

            [upSV addSubview:iv];

            

            //给图片添加点击事件

            UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(upPicClick:)];

            [iv addGestureRecognizer:tgr];

            [tgr release];

        }

        [self.view addSubview:upSV];

        [upSV release];

        

        //下面的大sv

        mainSV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 210, 320, 480-210)];

        mainSV.contentSize = CGSizeMake(320*4, 480-210);//设置区域

        mainSV.pagingEnabled = YES;

        for (int i = 0; i < 4; i++) {//把图片放进去

            UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, 480-210)];

            iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];

            [mainSV addSubview:iv];

        }

        mainSV.delegate = self;//设置代理

        [self.view addSubview:mainSV];

        

        NSArray *picArr = @[[UIImage imageNamed:@"btn_prepage"],[UIImage imageNamed:@"btn_home"],[UIImage imageNamed:@"btn_nextpage"]];

        for (int i = 0; i < 3; i++) {

            UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];

            bu.frame = CGRectMake(10+i*(92+45), 480-55, 45, 45);

            [bu setImage:[picArr objectAtIndex:i] forState:UIControlStateNormal];

            bu.tag = 100+i;

            [bu addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:bu];

        }

        

    }

     

    -(void)buttonClick:(UIButton *)sender

    {

        //当前是在第几页(从0开始)

        int f = (int)mainSV.contentOffset.x/320;

        

        if (sender.tag == 100) {

            //上一个

            if (f < 1) {

                //如果当前是第0个就不执行方法

                return;

            }

            f--;

        } else if (sender.tag == 101) {

            //首页

            f = 0;

        } else {

            //下一个

            if (f > 2) {

                //如果是最后一个就不执行

                return;

            }

            f++;

        }

        

        [mainSV setContentOffset:CGPointMake(320*f, 0) animated:YES];

        

        //从1开始

        self.navigationItem.title = [NSString stringWithFormat:@"第%d个",f+1];

    }

     

    - (void)upPicClick:(UITapGestureRecognizer *)tgr

    {//小图片的点击事件

        UIImageView *iv = (UIImageView *)tgr.view;

        

        self.navigationItem.title = [NSString stringWithFormat:@"第%d个",iv.tag-9];

        

        [mainSV setContentOffset:CGPointMake((iv.tag-10)*320, 0) animated:YES];

    }

     

    //delegate方法,scrollView停止滑动时调用

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

    {

        //contentOffset代表scrollView的偏移量,也就是x移动了多少,y移动了多少

        CGFloat f = scrollView.contentOffset.x;

        self.navigationItem.title = [NSString stringWithFormat:@"第%g个",f/320+1];

    }

     

     

    - (void)dealloc

    {

        [mainSV release];

        mainSV = nil;

        [super dealloc];

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    连接服务器时通时不通的问题
    二进制包搭建k8s集群
    2、带着问题学习JVM_对象存活_垃圾回收的内容_理论_收集算法
    四、redis事务_真正实现分布式Lua_分布式锁
    四、Spring 框架中接入单机Redis的两种方式
    3、通过zkCli⼯具命令和zk 的java API两种方式创建⼀个主从模式示列
    1、带着问题学习JVM_Java为什么属于编译型+解释型的高级语言_Class类文件结构理解
    2、zookeeper安装、常用命令、API说明及简单示列(创建ZK会话及对节点进行操作、创建分布式锁、创建全局唯一ID)
    1、zk的产生、优劣、基础知识
    十三、Spring容器的原理及源码分析
  • 原文地址:https://www.cnblogs.com/-yun/p/4379134.html
Copyright © 2020-2023  润新知