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的。爲什麼?
哈!感謝鈹,我永遠不會想到這一點!正在讓我瘋狂。 =) – Padin215