2011-11-28 47 views
1

我想的意见n个增加分页滚动视图功能。UIScrollView中添加的UIImageViews

如果点击“添加视图”按钮,说5次,全部5得到补充瓦特/出的UIImageViews。但如果我滑动到第二个视图并再次单击“添加视图”,则2个UIImageView会被添加到滚动视图(子视图)中。为什么在我滑动到下一个视图后添加这些图像视图?

这里我设置的观点:

- (void)viewDidLoad 
{  
    CGRect myRect = CGRectMake(0, 150, ([[UIScreen mainScreen] bounds].size.width), ([[UIScreen mainScreen] bounds].size.height - (64 + 150))); 

    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:myRect]; 
    myScrollView.backgroundColor = [UIColor lightGrayColor]; 
    myScrollView.pagingEnabled = TRUE; 
    myScrollView.showsHorizontalScrollIndicator = TRUE; 

    myRect = CGRectMake(20, 75, 150, 13); 

    myRect = CGRectMake(400, 50, 100, 50); 

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    myButton.frame = CGRectMake(400, 50, 100, 50); 
    myButton.titleLabel.text = @"get views"; 
    [myButton addTarget:self action:@selector(makePlantEvent:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:myButton]; 

    myRect = CGRectMake(0, 0, (768/2), (1004/2)); 

    [self.view addSubview:myScrollView]; 

    self.mainScrollView = myScrollView; 

    [super viewDidLoad]; 
} 

这里是我添加的观点方法:

-(IBAction)makePlantEvent:(id)sender 
{  
    NSLog(@"number of views in scrollview: %d", [self.mainScrollView.subviews count]); 
    NSLog(@"views in scrollView: %@", self.mainScrollView.subviews); 

    if ([self.mainScrollView.subviews count] == 0) 
    { 
     NSLog(@"adding first view"); 
     CGRect myRect = CGRectMake(10, 10, self.mainScrollView.frame.size.width - 20, self.mainScrollView.frame.size.height - 20); 

     UIView *myUIView = [[UIView alloc] initWithFrame:myRect]; 
     myUIView.backgroundColor = [UIColor whiteColor]; 
     myUIView.layer.borderWidth = 1.0f; 
     myUIView.layer.borderColor = [UIColor blackColor].CGColor; 

     self.mainScrollView.contentSize = CGSizeMake(768, mainScrollView.frame.size.height); 

     [self.mainScrollView addSubview:myUIView]; 
    } 

    else if ([self.mainScrollView.subviews count] >= 1) 
    { 
    NSLog(@"adding %d view", ([self.mainScrollView.subviews count] + 1)); 

     self.mainScrollView.contentSize = CGSizeMake((768 * ([self.mainScrollView.subviews count] + 1)), mainScrollView.frame.size.height); 

     CGRect myRect = CGRectMake((768 * [self.mainScrollView.subviews count]) + 10, 10, self.mainScrollView.frame.size.width - 20, self.mainScrollView.frame.size.height - 20); 

     UIView *myUIView = [[UIView alloc] initWithFrame:myRect]; 
     myUIView.backgroundColor = [UIColor blueColor]; 
     myUIView.layer.borderWidth = 1.0f; 
     myUIView.layer.borderColor = [UIColor blackColor].CGColor; 

     [self.mainScrollView addSubview:myUIView]; 
    } 

    NSLog(@"at end number of views in scrollview: %d", [self.mainScrollView.subviews count]); 
    NSLog(@"at end views in scrollView: %@", self.mainScrollView.subviews); 
} 

我的输出: 加2次

2011-11-28 13:42:39.976 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 0 
2011-11-28 13:42:39.977 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
) 
2011-11-28 13:42:39.978 Inspect[87637:207] adding first view 
2011-11-28 13:42:39.978 Inspect[87637:207] at end number of views in scrollview: 1 
2011-11-28 13:42:39.979 Inspect[87637:207] at end views in scrollView: (
    "<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>" 
) 
2011-11-28 13:42:41.652 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 1 
2011-11-28 13:42:41.653 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
    "<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>" 
) 
2011-11-28 13:42:41.654 Inspect[87637:207] adding 2 view 
2011-11-28 13:42:41.654 Inspect[87637:207] at end number of views in scrollview: 2 
2011-11-28 13:42:41.655 Inspect[87637:207] at end views in scrollView: (
    "<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>", 
    "<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>" 
) 

轻扫第二次查看并再次单击按钮:

2011-11-28 13:42:49.628 Inspect[87637:207] BEGINING OF METHOD number of views in scrollview: 4 
2011-11-28 13:42:49.629 Inspect[87637:207] BEGINING OF METHODviews in scrollView: (
    "<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>", 
    "<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>", 
    "<UIImageView: 0x108016d0; frame = (760 1; 7 808); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x10800200>>", 
    "<UIImageView: 0x10801690; frame = (1152 802; 383 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x108000a0>>" 
) 
2011-11-28 13:42:49.630 Inspect[87637:207] adding 3 view 
2011-11-28 13:42:49.630 Inspect[87637:207] at end number of views in scrollview: 5 
2011-11-28 13:42:49.631 Inspect[87637:207] at end views in scrollView: (
    "<UIView: 0x8874140; frame = (10 10; 748 790); layer = <CALayer: 0x8877d60>>", 
    "<UIView: 0x8a1f390; frame = (778 10; 748 790); layer = <CALayer: 0x8a26d70>>", 
    "<UIImageView: 0x108016d0; frame = (760 1; 7 808); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x10800200>>", 
    "<UIImageView: 0x10801690; frame = (1152 802; 383 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x108000a0>>", 
    "<UIView: 0xf4002f0; frame = (3082 10; 748 790); layer = <CALayer: 0xf4001c0>>" 

所以刷卡将两个UIImageViews的。为什么?

回答