Tuesday, June 4, 2013

UIScrollView scrollEnabled example in Objective C (iOS).


UIScrollView scrollEnabled

A Boolean value that determines whether scrolling is enabled.

@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled

Discussion of [UIScrollView scrollEnabled]
If the value of this property is YES , scrolling is enabled, and if it is NO , scrolling is disabled. The default is YES.

When scrolling is disabled, the scroll view does not accept touch events; it forwards them up the responder chain.

UIScrollView scrollEnabled example.
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  if (scrollView.zoomScale!=1.0) {
    // Zooming, enable scrolling
    scrollView.scrollEnabled = TRUE;
  } else {
    // Not zoomed, disable scrolling so gestures get used instead
    scrollView.scrollEnabled = FALSE;
  }
}

Example of [UIScrollView scrollEnabled].
- (void)viewDidLoad {
  [super viewDidLoad];
  myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
  myScrollView.maximumZoomScale = 4.0;
  myScrollView.minimumZoomScale = 1.0;
  myScrollView.clipsToBounds = YES;
  myScrollView.delegate = self;

  [myScrollView addSubview:myImage];
  [self setWantsFullScreenLayout:TRUE];

  myScrollView.scrollEnabled = FALSE;
  UISwipeGestureRecognizer *recognizer =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
  recognizer.delaysTouchesBegan = TRUE;
  [myScrollView addGestureRecognizer:recognizer];
  [recognizer release];

  recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
  recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
  [myScrollView addGestureRecognizer:recognizer];
  [recognizer release];
  [myScrollView delaysContentTouches];
}

UIScrollView scrollEnabled example.
if (scrollView.contentSize.height>scrollView.frame.size.height) {
    scrollView.scrollEnabled = YES;
}
else {
    scrollView.scrollEnabled = NO;
}

End of UIScrollView scrollEnabled example article.