zoukankan      html  css  js  c++  java
  • 【代码笔记】iOS-点击加号增加书架,点击减号减少书架

    一,效果图。

    二,工程图。

    三,代码。

    ReaderViewController.h

    复制代码
    #import <UIKit/UIKit.h>
    
    @interface ReaderViewController : UIViewController
    <UIScrollViewDelegate>
    {
        UIScrollView *scrollView;
    }
    @property(nonatomic,strong) UIScrollView *scrollView;;
    -(void)add;
    -(void)dele;
    -(void)reloadView;
    
    @end
    复制代码

     

    ReaderViewController.m

    复制代码
    #import "ReaderViewController.h"
    
    #define ImageHeight 120
    static int num = 0;
    
    @interface ReaderViewController ()
    
    @end
    
    
    @implementation ReaderViewController
    
    - (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.
        
        //字体的颜色
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:248.0/255.0 green:172.0/255.0 blue:37.0/255.0 alpha:1.0];
        //导航栏背景图
        [self addBackgroundView:@"1.png"];
        
        //左侧删除按钮
        UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]
                                           initWithTitle:@"delete"
                                           style:UIBarButtonItemStyleBordered
                                           target:self
                                           action:@selector(dele)];
        //右侧增加按钮
        UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]
                                          initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                          target:self
                                          action:@selector(add)];
        
        self.navigationItem.rightBarButtonItem = rightBarButton;
        self.navigationItem.leftBarButtonItem = leftBarButton;
        
        //scrollerView
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
        scrollView.contentSize=CGSizeMake(320,480);
        
        //设置背景颜色
        UIImage *backimg=[UIImage imageNamed:@"3.png"];
        UIImageView *backview=[[UIImageView alloc] initWithImage:backimg];
        backview.frame=CGRectMake(0, 0, 320, 480);
        [self.view addSubview:backview];
        
        [self.view addSubview:scrollView];
        //重新刷新界面
        [self reloadView];
    
    }
    //导航栏上标题
    -(void)addBackgroundView:(NSString*)image{
        
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]];
        imgView.frame = CGRectMake(0, 0, 320, 44);
        [self.navigationController.navigationBar addSubview: imgView];
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake((320-150)/2, 0, 150, 44)];
        [title setText:@"书架"];
        [title setTextColor:[UIColor whiteColor]];
        [title setTextAlignment:NSTextAlignmentCenter];
        [title setShadowColor:[UIColor grayColor]];
        [title setBackgroundColor:[UIColor clearColor]];
        [title setFont:[UIFont boldSystemFontOfSize:20.0]];
        [self.navigationController.navigationBar addSubview:title];
    }
    
    //添加一个书架
    -(void)add{
        
        num++;
        
        UIImage *backimg=[UIImage imageNamed:@"2.png"];
        UIImageView *backview=[[UIImageView alloc] initWithImage:backimg];
        backview.frame=CGRectMake(0, (num-1)*ImageHeight, 320, ImageHeight);
        backview.tag = num+100;
        
        [scrollView addSubview:backview];
        
        if (num>3) {
            scrollView.contentSize=CGSizeMake(320,num*ImageHeight);
        }
    }
    
    //删除一个书架
    -(void)dele{
        
        if (num<=0) {
            return;
        }
        
        for (UIView *view in scrollView.subviews) {
            if(view.tag == (num+100))
                [view removeFromSuperview];
        }
        
        num--;
        
        if (num>3) {
            scrollView.contentSize=CGSizeMake(320,num*ImageHeight);
        }
        
    }
    
    //主界面初始化
    -(void)reloadView{
        
        NSLog(@"reloadView");
        //遍历当前界面的所有子界面,把子界面删除干净
        for(UIView *view in scrollView.subviews){
            [view removeFromSuperview];
        }
        
        scrollView.showsVerticalScrollIndicator=NO;
        scrollView.showsHorizontalScrollIndicator=NO;
        scrollView.delegate=self;
        [scrollView setScrollEnabled:YES];
        
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    复制代码

     

  • 相关阅读:
    python 多进程-03 进程池
    python 多进程-02 进程间数据共享
    python 多进程-01 基本使用
    python 多线程-02 线程池
    开发者入驻
    小程序组件 scroll-view 下拉加载更多,触底触发事件,容器高度设置
    Array.from在360浏览器和IE浏览器兼容问题
    获取cookie
    背景图左右居中
    uniapp使用uni.setStorageAsync刷新页面数据丢失问题
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/5112052.html
Copyright © 2011-2022 走看看