gpt4 book ai didi

iphone - UIScrollView 添加 UIImageViews

转载 作者:行者123 更新时间:2023-11-29 11:18:34 25 4
gpt4 key购买 nike

我想在启用分页的 ScrollView 中添加 n 个 View 。

如果点击“添加 View ”按钮说 5 次,所有 5 次都会添加 w/out uiimageviews。但是如果我滑动到第二个 View 并再次单击“添加 View ”,则会将 2 个 UIImageView 添加到 ScrollView ( subview )中。为什么在我滑动到下一个 View 后添加了这些 ImageView ?

这里我设置了 View :

- (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];
}

这是我添加 View 的方法:

-(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 个 View

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>>"
)

滑动到第二个 View 并再次点击按钮:

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>>"

所以滑动添加了两个 UIImageView。为什么?

最佳答案

有滚动指示器。当您的 scrollview contentSize > scrollview size 时,UIScrollView 会自动向自身添加两个 subview (UIImageView)。

要禁用此功能集 showsHorizontalScrollIndicatorshowsVerticalScrollIndicator属性为 NO。

关于iphone - UIScrollView 添加 UIImageViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8301477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com