UIScrollView에 UIWebView, UITableView를 서브뷰로 추가할 경우 터치 이벤트로 인한 오동작이 발생할 수 있으므로 이런 방식의 구현은 피해야한다.


ScrollView content
01
// Content Offset
02
[scrollView setContentOffset:CGPointMake(100.0f, 100.0f)];
03
CGPoint offset = [scrollView contentOffset];
04
 
05
// Content Size
06
CGSize size = [scrollView contentSize];
07
scrollView.contentSize = CGSizeMake(320, 480);
08
 
09
// Content Inset
10
// Content 상하에 여백을 추가한다.
11
scrollView.contentInset = UIEdgeInsetsMake(64.0,0.0,44.0,0.0);


Scrolling
01
// 스크롤 설정
02
scrollView.scrollEnabled = YES;
03
 
04
// 수직/수평 한 방향으로만 스크롤 되도록 설정
05
scrollView.directionalLockEnabled = YES;
06
 
07
// 상태바를 클릭할 경우 가장 위쪽으로 스크롤 되도록 설정. scrollViewDidScrollToTop: 델리게이트 메소드로 적절한 처리를 해 주어야 한다.
08
scrollView.scrollsToTop = YES;
09
 
10
// 특정 사각 영역이 뷰에 표시되도록 오프셋 이동
11
[scrollView scrollRectToVisible:CGRectMake(50.0, 50.0, 100.0, 100.0) animated:YES];
12
 
13
// 페이징 설정
14
scrollView.pagingEnabled = NO;
15
 
16
// 스크롤이 경계를 넘어섰다가 다시 복귀했는지 판별
17
if (scrollView.bounces) {
18
    NSLog(@"Bounce");
19
}
20
 
21
// 스크롤이 경계에 도달하면 바운싱효과를 적용
22
scrollView.alwaysBounceHorizontal = YES;
23
scrollView.alwaysBounceVertical = YES;
24
 
25
// 스크롤뷰를 터치할 경우 컨텐츠 내부의 뷰에 이벤트 전달
26
[scrollView touchesShouldBegin:touches withEvent:evt inContentView:contentInView];
27
[scrollView touchesShouldCancelInContentView:contentInView];
28
 
29
// 터치이벤트가 발생할 경우 딜레이를 두고 touchesShouldBegin:withEvent:inContentView: 메소드를 호출
30
scrollView.delaysContentTouches = YES;
31
 
32
// 감속 속도 조절
33
scrollView.decelerationRate = UIScrollViewDecelerationRateFast; // or UIScrollViewDecelerationRateNormal
34
 
35
// 스크롤을 하기 위해 뷰를 터치했는지 판별
36
if (scrollView.tracking) {
37
    NSLog(@"User has touched the content view but might not have yet have started dragging it");
38
}
39
 
40
// 스크롤이 시작되었는지 판별
41
if (scrollView.dragging) {
42
    NSLog(@"Dragging or Scrolling....");
43
}
44
 
45
// 스크롤이 종료되고 감속중인지 판별
46
if (scrollView.decelerating) {
47
    NSLog(@"User isn't dragging the content but scrolling is still occurring");
48
}
49
 
50
// 스크롤 바 스타일
51
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
52
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
53
scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
54
 
55
// 스크롤 바 표시
56
scrollView.showsHorizontalScrollIndicator = YES;
57
scrollView.showsVerticalScrollIndicator = YES;
58
 
59
// 특정 시점에 스크롤바 표시
60
[scrollView flashScrollIndicators];
61
 
62
// 스크롤 바 위치 설정
63
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);


Zooming & Panning
01
// 특정 영역을 스크롤뷰에 꽉 차도록 확대/축소 한다.
02
[scrollView zoomToRect:CGRectMake(0.0, 0.0, 20.0, 20.0) animated:YES];
03
 
04
// 현재 줌 배율
05
float fZoomScale = scrollView.zoomScale;
06
 
07
// 줌 배율 설정
08
scrollView.minimumZoomScale = 0.2;
09
scrollView.maximumZoomScale = 10.0;
10
[scrollView setZoomScale:3.0 animated:YES];
11
 
12
// 스크롤 뷰가 바운싱되었는지 판별
13
if (scrollView.zoomBouncing) {
14
    NSLog(@"The scroll view is zooming back to a minimum or maximum zoom scaling value");
15
}
16
 
17
// 스크롤 뷰가 줌 중인지 판별
18
if (scrollView.zooming) {
19
    NSLog(@"User is making a zoom gesture");
20
}
21
 
22
// 바운싱 줌 활성화
23
scrollView.bouncesZoom = YES;


UIScrollViewDelegate
view sourceprint?
01
#pragma mark -
02
#pragma mark UIScrollViewDelegate Protocol Methods
03
// 스크롤뷰가 스크롤 된 후에 실행된다.
04
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
05
{
06
}
07
 
08
// 스크롤뷰가 스크롤 되기 직전에 실행된다.
09
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
10
{
11
}
12
 
13
// 스크린을 드래그한 직후에 실행된다.
14
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
15
{
16
    if (decelerate) {
17
        NSLog(@"The scrolling movement will continue, but decelerate, after a touch-up gesture during a dragging operation");
18
    } else {
19
        NSLog(@"Scrolling stops immediately upon touch-up");
20
    }
21
}
22
 
23
// 스크롤뷰가 가장 위쪽으로 스크롤 되기 전에 실행된다. NO를 리턴할 경우 위쪽으로 스크롤되지 않도록 한다.
24
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
25
{
26
    return YES;
27
}
28
 
29
// 스크롤뷰가 가장 위쪽으로 스크롤 된 후에 실행된다.
30
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
31
{
32
}
33
 
34
// 스크롤뷰가 Touch-up 이벤트를 받아 스크롤 속도가 줄어들때 실행된다.
35
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
36
{
37
}
38
 
39
// 스크롤 애니메이션의 감속 효과가 종료된 후에 실행된다.
40
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
41
{
42
}
43
 
44
// 줌 효과를 적용할 뷰를 리턴한다.
45
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
46
{
47
    return webView;
48
}
49
 
50
// 줌이 시작될때 실행된다.
51
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView*)view
52
{
53
 
54
}
55
 
56
// 줌을 완료했을때 실행된다.
57
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
58
{
59
}
60
 
61
// 줌 스케일이 변경된 후에 실행된다.
62
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
63
{
64
}
65
 
66
// 스크롤 애니메이션이 종료된 후에 실행된다.
67
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
68
{
69
}

      
Posted by k_ben