[Code Note] Click the plus sign to add a bookshelf, click the minus sign to reduce the bookshelf, And the minus sign to the bookshelf

Source: Internet
Author: User

[Code Note] Click the plus sign to add a bookshelf, click the minus sign to reduce the bookshelf, And the minus sign to the bookshelf

I ,.


2. Engineering Drawing.


3. Code.

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.
    
    // font color
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed: 248.0 / 255.0 green: 172.0 / 255.0 blue: 37.0 / 255.0 alpha: 1.0];
    // Navigation bar background image
    [self addBackgroundView: @ "1.png"];
    
    // left delete button
    UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc]
                                       initWithTitle: @ "delete"
                                       style: UIBarButtonItemStyleBordered
                                       target: self
                                       action: @selector (dele)];
    // Add button on the right
    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);
    
    // Set the background color
    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];
    // Refresh the interface
    [self reloadView];

}
// Title on the navigation bar
-(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];
}

// Add a bookshelf
-(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);
    }
}

// delete a bookshelf
-(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);
    }
    
}

// Main interface initialization
-(void) reloadView {
    
    NSLog (@ "reloadView");
    // Iterate through all sub-interfaces of the current interface and delete the sub-interfaces clean
    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.
} 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.