0

想要在图像上显示UIButton完成。UIScrollView显示了下面的代码UIButton,但是当我滚动UIImageViews 时,它仅显示在第一个 imageview 上,正如我想要UIButton完成的那样,它应该显示在UIScrollView.

 - (void)viewDidLoad

 {
 self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

[myButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];

myButton.frame = CGRectMake(0, 0, 60, 35);

[myButton.layer setMasksToBounds:YES];

[myButton.layer setCornerRadius:10.0f];

myButton.layer.borderWidth = 2;

myButton.layer.borderColor = [[UIColor whiteColor] CGColor];

[myButton setTitle:@"Done" forState:UIControlStateNormal];

myButton.backgroundColor = [UIColor blackColor];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];

[[self navigationItem] setRightBarButtonItem:button animated:YES];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat xOrigin = i * self.view.frame.size.width;

    NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
    [imageScrollView addSubview:imageView];
    [imageScrollView addSubview:myButton];
    [imageView release];
   }
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
 }

当我滚动它时,我应该如何编码它UIButton应该在所有图像视图上显示。

4

2 回答 2

1

添加您的UIButtonin for 循环,即每次添加新ImageView的时它都可以添加。x_Origin并为每个更改它imageView-

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    imageScrollView.pagingEnabled = YES;
    NSInteger numberOfViews = 61;

    for (int i = 0; i < numberOfViews; i++) 
    {
        CGFloat xOrigin = i * self.view.frame.size.width;
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [myButton addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
        myButton.frame = CGRectMake(xOrigin, 0, 60, 35);
        [myButton setTitle:@"Done" forState:UIControlStateNormal];
        myButton.backgroundColor = [UIColor blackColor];
        [myButton.layer setMasksToBounds:YES];

       [myButton.layer setCornerRadius:10.0f];
        myButton.layer.borderWidth = 2;
        myButton.layer.borderColor = [[UIColor whiteColor] CGColor];

        NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
        [imageScrollView addSubview:imageView];
        [imageScrollView addSubview:myButton];
    }

    imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
    [self.view addSubview:imageScrollView];

}

删除这 2 行

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];

没有必要这样做。这意味着您正在设置该按钮rightBarButtonNavigationBar但在您的情况下,我认为您不想这样做。

于 2012-10-03T13:21:27.550 回答
0

在 for 循环中为 myButton 设置框架,类似于 imageview 框架。

myButton.frame= CGRectMake(xOrigin, 0, self.myButton.frame.size.width, self.myButton.frame.size.height);
于 2012-10-03T12:55:23.547 回答