在UIScrollview上显示UIButton Done以获取图像

时间:2012-10-03 12:41:35

标签: ios iphone objective-c ios6 uiscrollview

想要在UIButton上针对图片展示UIScrollView。下面的代码UIButton正在显示,但是当我滚动UIImageView时,它只会在第一个图像视图中显示我想要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进行编码,以便在滚动时在所有图片视图中显示。

2 个答案:

答案 0 :(得分:1)

UIButton添加到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];

没有必要这样做。这意味着您在rightBarButton的{​​{1}}上设置了该按钮,但在您的情况下我认为您不想这样做。

答案 1 :(得分:0)

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

myButton.frame= CGRectMake(xOrigin, 0, self.myButton.frame.size.width, self.myButton.frame.size.height);