显示自定义查看问题

时间:2017-04-26 07:56:16

标签: ios objective-c

enter image description here output image

当我点击按钮时,我只获得了一个额外的视图,但我想要一个动态添加视图的点击次数,(比如for循环)...请帮我这个代码,谢谢你预先下, 这只是我的代码的一部分

if (boolVal == true) {
    CGRect newFrameC = CGRectMake(_centreView.frame.origin.x, _centreView.frame.origin.y, _centreView.frame.size.width, 50);
    CGRect newFrameL1 = CGRectMake(_label2.frame.origin.x, _label2.frame.origin.y+50, _label2.frame.size.width, 50);
    _centreView.frame = newFrameC;
    _label2.frame = newFrameL1;
    boolVal = false;
}else if (boolVal == false){
    CGRect newFrameC = CGRectMake(_centreView.frame.origin.x, _centreView.frame.origin.y, _centreView.frame.size.width, 1);
    CGRect newFrameL1 = CGRectMake(_label2.frame.origin.x, _label2.frame.origin.y-50, _label2.frame.size.width, 50);
    _centreView.frame = newFrameC;
    _label2.frame = newFrameL1;
    boolVal = true;
}

1 个答案:

答案 0 :(得分:0)

<强> DummyViewController.m

#import "DummyViewController.h"
#import "ExtraView.h"

@interface DummyViewController ()

@property (nonatomic) unsigned int numberOfExtraViews;
@property (nonatomic, strong) NSMutableArray<ExtraView*>* extraViews;
@property (nonatomic, strong) UILabel* label1;
@property (nonatomic, strong) UILabel* label2;

@end

@implementation DummyViewController

-(void) removeExtraViews{
    for (ExtraView* extraView in _extraViews){
        [extraView removeFromSuperview];
    }
    [_extraViews removeAllObjects];
}

-(CGRect) getExtraViewFrame{
    CGRect extraViewFrame = _label1.frame;
    if (_numberOfExtraViews > 0) {
        extraViewFrame = [_extraViews lastObject].frame;
    }
    extraViewFrame.origin.x += extraViewFrame.size.height;
    return extraViewFrame;
}

- (void) addExtraViews{
    //[self removeExtraViews];
    int numberOfExtraViewsToDraw = _numberOfExtraViews - _extraViews.count;

    for (int iterator = 0; iterator < numberOfExtraViewsToDraw; iterator ++){
        CGRect extraViewFrame = [self getExtraViewFrame];
        ExtraView *extraView = [[ExtraView alloc]initWithFrame:extraViewFrame];
        [self.view addSubview:extraView];
        [_extraViews addObject:extraView];
    }

    if (numberOfExtraViewsToDraw > 0) {
        CGRect label2Frame = [_extraViews lastObject].frame;
        label2Frame.origin.x += label2Frame.size.height;
        _label2.frame = label2Frame;
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addExtraViews];
    //rest of your code
}

//Use this code in initWithNib/initWithCoder. Don't copy paste the same
-(instancetype)init{
    self = [super init];
    if (nil != self){
        _numberOfExtraViews = 0;
        _extraViews = [[NSMutableArray alloc]init];
    }
    return self;
}

//the function that gets hit when the button is tapped.
- (void) onButtonTap{
    _numberOfExtraViews++;
    [self.view setNeedsDisplay];
}

请注意以下事项:

  1. 我不明白boolVal是什么,但是如果它在点击按钮时不停地翻转,你将永远不会有多个视图。我认为它会切换,所以我根本没用过它。
  2. 在initWithNib / initWithCoder中使用init代码。别复制粘贴。不要覆盖给定的代码。只需附加到当前的init。
  3. 在我的代码中,onButtonTap是点击按钮时被点击的功能。
  4. 我已经通过确保viewDidLoad方法获得了agin来完成它。但我认为这不是必要的。你也可以这样做:

    //the function that gets hit when the button is tapped.
    - (void) onButtonTap{
        _numberOfExtraViews++;
        [self addExtraViews];
    }
    
  5. 已完成的工作:

    1. 让我将您称之为centerView的内容称为extraView。会有很多额外的视图,所以我会为同一个(extraViews)创建一个数组。
    2. 视图数量的计数存储在numberOfExtraViews中。在init。中启动为0。
    3. 每当点击按钮时,我们都会增加计数并调用视图的setNeedsDisplay,后者又会点击viewDidLoad方法
    4. 在viewDidLoad中,我们添加了extraViews。