UIScrollView为self.view添加缩放

时间:2013-09-12 16:19:26

标签: ios objective-c uiscrollview

我需要缩放视图中的所有对象,但丢失对象的位置(UIButton)

- (void)viewDidLoad
    {

        CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
        scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
        scrollView.delegate = self;

        scrollView.minimumZoomScale = 1;
        scrollView.maximumZoomScale = 4.0;


        [self.view addSubview:scrollView];
        CGSize scrollViewContentSize = CGSizeMake(640, 800);
        [scrollView setContentSize:scrollViewContentSize];



        NSMutableArray *datos = [[NSMutableArray alloc]initWithObjects:@"ob1",@"ob2",@"ob3",@"ob4", nil];

         for (int i = 0; i < [datos count]; i++) {


          UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          [button addTarget:self action:@selector(aMethod)
          forControlEvents:UIControlEventTouchUpInside];
           button.frame = CGRectMake(100, 100*i, 70, 70);
           button.tag = i;
          [button setTitle: @"Line1" forState: UIControlStateNormal];
          button.hidden= NO;
          [scrollView addSubview:button];
          }



 }


-(void)aMethod{ 

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

     return scrollView.superview;
}

1 个答案:

答案 0 :(得分:1)

您应该设置一个子视图容器并将其作为viewForZoomingInScrollView传递。

子视图应包含您的按钮:

self.buttonSubview = [[UIView alloc] initWithFrame:...];
...
[buttonSubview addSubview:button];
...
[scrollView addSubview:buttonSubview];

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
     return self.buttonSubview;
}