首页 > 代码库 > 加号增加书架,减号减少书架(code4App)
加号增加书架,减号减少书架(code4App)
开始时候的效果图:
点击增加时候的效果图:
点击减少时候的效果图:
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 120static 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]; }
加号增加书架,减号减少书架(code4App)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。