2013-01-08 34 views
2

在我正在研究的其中一个项目中,我有一个UIScrollView,它有许多类似于电影院座位的按钮和标签,用户可以点击以选择座位供他们购买。是否可以放大UIScrollView?我已经宣布UIScrollViewDelegate,将delegate设置为self,设置最小和最大缩放比例,但我遇到viewForZoomingInScrollViewscrollViewDidEndZooming方法的问题。我试图在viewForZoomingInScrollView方法上返回我的滚动视图,但它崩溃。放大Objective-C上的UIScrollview

下面是一些代码:

- (void) renderSeatMapOnScreen 
{  
    int x_offset_row = 10; 
    int y_offset_row = 25; 
    int x_offset_col = 30; 
    int y_offset_col = 0; 
    int scrollview_w = 0; 
    int scrollview_h = 0; 

    NSString *row_name = @""; 
    int tag_index = 0; 

    NSMutableArray *seat_map_table = [seat_map_xml seats_map]; 
    NSString *seat_available_str = [seat_avail_xml seat_available]; 

    HideNetworkActivityIndicator(); 

    NSLog(@"Seats available = %@", seat_available_str); 

    NSArray *seats_avail = [seat_available_str componentsSeparatedByString:@";"]; 

    int ROW_COL_OFFSET = 25; 

    for (int row = 0; row < [seat_map_table count]; row++) 
    { 
     UILabel * rowlabel = [[UILabel alloc] init]; 
     rowlabel.frame = CGRectMake(x_offset_row , y_offset_row, 22, 22); 

     SeatMapDeclaration *rowmap = [seat_map_table objectAtIndex:row]; 
     rowlabel.text = rowmap.rows; 
     [scrollview addSubview:rowlabel]; 

     row_name = [NSString stringWithFormat:@"%@", rowmap.rows]; 

     NSArray *seat = [rowmap.rowmap componentsSeparatedByString:@";"]; 
     NSLog(@"row[%i] = %@", row, rowmap.rowmap); 

     if (row == 0) 
     { 
      scrollview_w = [seat count] * ROW_COL_OFFSET + y_offset_row; 
      total_column = [seat count]; 
      total_row = [seat_map_table count]; 
     } 

     x_offset_col = 30 ; 
     y_offset_col = y_offset_row ; 

     for (int column = 0; column < [seat count]; column++) 
     { 
      if (([[seat objectAtIndex:column] rangeOfString:@"a("].location != NSNotFound) 
       || ([[seat objectAtIndex:column] isEqualToString:@""])) 
      { 
       CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22); 
       UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect]; 
       [imageView setImage:[UIImage imageNamed:@"noseat.png"]]; 
       [scrollview addSubview:imageView]; 
      } 
      else if ([[seat objectAtIndex:column] rangeOfString:@"b("].location != NSNotFound) 
      { 
       CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22); 
       UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect]; 
       imageView.image = [UIImage imageNamed:@"noseat.png"]; 
       [scrollview addSubview:imageView]; 
      } 
      else 
      { 
       NSString *status = [NSString stringWithFormat:@"%@%@", row_name, [seat objectAtIndex:column]]; 

       BOOL matchedFound = NO; 
       for (int i = 0; i < [seats_avail count]; i++) 
       { 
        if ([[seats_avail objectAtIndex:i] isEqualToString:status]) 
        { 
         matchedFound = YES ; 
         break ; 
        } 
       } 

       if (matchedFound == YES) 
       { 
        UIButton * seat_btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
        [seat_btn setBackgroundImage:[UIImage imageNamed:@"seatavailable.png"] forState:UIControlStateNormal]; 
        [seat_btn setBackgroundImage:[UIImage imageNamed:@"seatactive.png"] forState:UIControlStateSelected]; 
        seat_btn.frame = CGRectMake(x_offset_col, y_offset_col, 22, 22); 
        tag_index = [[seat objectAtIndex:column] intValue]; 
        [seat_btn setTitle:[seat objectAtIndex:column] forState:UIControlStateNormal]; 
        seat_btn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0]; 
        seat_btn.tag = tag_index + row * 100; 
        [seat_btn addTarget:self 
           action:@selector(checkboxButton:) 
         forControlEvents:UIControlEventTouchUpInside]; 
        [scrollview addSubview:seat_btn]; 

       } 
       else 
       { 
        CGRect myImageRect = CGRectMake(x_offset_col, y_offset_col, 22, 22); 
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect]; 
        imageView.image = [UIImage imageNamed:@"seat_not_available.png"]; 
        [scrollview addSubview:imageView]; 
       } 
      } 

      x_offset_col += ROW_COL_OFFSET ; 

      NSString *data = @""; 
      [seatControl addObject:data]; 
     } 
     y_offset_row += ROW_COL_OFFSET; 
    } 

    UILabel *screenlabel = [[UILabel alloc] init]; 
    screenlabel.frame = CGRectMake((scrollview_w-300)/2, 3, 300, 15); 
    screenlabel.text = @"Screen"; 
    screenlabel.backgroundColor = [UIColor blackColor]; 
    screenlabel.textColor = [UIColor whiteColor] ; 
    screenlabel.font = [UIFont fontWithName:@"Helvetica" size:10.0]; 
    screenlabel.textAlignment = UITextAlignmentCenter; 
    [scrollview addSubview:screenlabel]; 

    scrollview_h = y_offset_row + ROW_COL_OFFSET; 
    // Adjust scroll view 
    [scrollview setContentSize:CGSizeMake(scrollview_w, scrollview_h)]; 
} 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    NSLog(@"1"); 

    return scrollview; 
} 

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale 
{ 
    NSLog(@"2"); 
} 
+0

offtopic:你应该看一看到ObjC编码指南:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html – CarlJ

+0

分享崩溃报告..启用异常断点来获取更多细节和确切的崩溃点。 –

+0

当方法viewForZoomingInScrollView被调用时它崩溃。 – jaytrixz

回答

1

在:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{ 
    NSLog(@"1"); 

    return scrollview; 
} 

你不应该返回UIScrollView本身,而不是它的子视图,你想滚动/缩放。

如果你有很多子视图,我会建议你创建一个容器视图为你从viewForZoomingInScrollView返回的所有视图;而不是将您的席位添加到滚动视图本身,然后将它们添加到容器视图。

+0

谢谢。这会使用户捏和放大视图,仍然能够点击按钮?另外,我还需要使用scrollViewDidEndZooming方法吗? – jaytrixz

+0

是的,它会的。关于'scrollViewDidEndZooming'这取决于你想在那里做什么......试一试,看看你需要做些什么才能使事情顺利进行,但是很有可能,它会正常工作...... – sergio

0

如果要放大到一定的座位在UIScrollView,你应该使用zoomToRect: animated:。您可以指定要放大的特定矩形(即座位)。

+0

我其实会希望用户捏放大。这种方法能让我做同样的事情吗? – jaytrixz

0

仅当您实现viewForZoomingInScrollView:委托回调时,缩放才起作用。

UIImageView *tempImage = [[UIImageView alloc]initWithImage:[UIImage imageWithData:data]]; 
self.imageView = tempImage; 

scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height); 
scrollView.maximumZoomScale = 1; 
scrollView.minimumZoomScale = .37; 
scrollView.clipsToBounds = YES; 
scrollView.delegate = self; 
[scrollView addSubview:imageView]; 
scrollView.zoomScale = .37; 

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)inScroll { 
    return imageView; 
}