Note that there are some explanatory texts on larger screens.

plurals
  1. POUIScrollView Zooming Crashes App with Exc_Bad_Access
    primarykey
    data
    text
    <p>I am getting a Exc_Bad_Access error in main when I try to scroll my UIScrollView. This program is simple. I have A UIViewController added in AppDelegate. Then, here is the code in the ViewController. This works fine for scrolling but as soon as I add <code>tmpScrollView.delegate = self</code>. The program won't scroll or zoom and gives me the Exc_Bad_Access error when I try to zoom.</p> <pre><code>@interface MainViewController : UIViewController &amp;ltUIScrollViewDelegate&gt; @property (nonatomic,retain) UIScrollView *tmpScrollView; @property (nonatomic,retain) UIView *containerView; @end - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. tmpScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; tmpScrollView.delegate = self; [self.view addSubview:tmpScrollView]; // Set up the container view to hold our custom view hierarchy CGSize containerSize = CGSizeMake(640.0f, 640.0f); self.containerView = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}]; [self.tmpScrollView addSubview:self.containerView]; // Set up custom view hierarchy UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 640.0f, 80.0f)]; redView.backgroundColor = [UIColor redColor]; [self.containerView addSubview:redView]; UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 560.0f, 640.0f, 80.0f)]; blueView.backgroundColor = [UIColor blueColor]; [self.containerView addSubview:blueView]; UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 160.0f, 320.0f, 320.0f)]; greenView.backgroundColor = [UIColor greenColor]; [self.containerView addSubview:greenView]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slow.png"]]; imageView.center = CGPointMake(320.0f, 320.0f); [self.containerView addSubview:imageView]; //Tell the scroll view the size of the contents self.tmpScrollView.contentSize = containerSize; } -(void)viewWillAppear:(BOOL)animated { NSLog(@"Will Appear"); [super viewWillAppear:animated]; // Set up the minimum &amp; maximum zoom scales CGRect scrollViewFrame = self.tmpScrollView.frame; CGFloat scaleWidth = scrollViewFrame.size.width / self.tmpScrollView.contentSize.width; CGFloat scaleHeight = scrollViewFrame.size.height / self.tmpScrollView.contentSize.height; CGFloat minScale = MIN(scaleWidth, scaleHeight); self.tmpScrollView.minimumZoomScale = minScale; self.tmpScrollView.maximumZoomScale = 1.0f; self.tmpScrollView.zoomScale = minScale; //***** Here is the line for setting the delegate self.tmpScrollView.delegate = self; [self centerScrollViewContents]; } -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale { NSLog(@"End Zoom"); } -(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView { // Return the view that we want to zoom return self.containerView; } -(void)scrollViewDidZoom:(UIScrollView *)scrollView { // The scroll view has zoomed, so we need to re-center the contents [self centerScrollViewContents]; } @end </code></pre> <p>Thanks for any help. This has been driving me crazy for a day now...</p> <p>Edit: Adding centerScrollViewContens too:</p> <pre><code>- (void)centerScrollViewContents { CGSize boundsSize = self.tmpScrollView.bounds.size; CGRect contentsFrame = self.containerView.frame; if (contentsFrame.size.width &lt; boundsSize.width) { contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f; } else { contentsFrame.origin.x = 0.0f; } if (contentsFrame.size.height &lt; boundsSize.height) { contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f; } else { contentsFrame.origin.y = 0.0f; } self.containerView.frame = contentsFrame; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload