#import "FJViewController.h"動畫
@interface FJViewController ()<UIScrollViewDelegate>spa
@end .net
@implementation FJViewController3d
- (void)viewDidAppear:(BOOL)animated{代理
[super viewDidAppear:animated];orm
UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:101];對象
//動畫去更新偏移量圖片
[scroll setContentOffset:CGPointMake(1000, 700) animated:YES];ci
}get
- (void)viewDidLoad {
[super viewDidLoad];
//1.建立UIScrollView對象
//建立UIScrollView的時候,系統會自動給UIScrollView
//建立一個子視圖,專門負責UIScrollView上內容(UIScrollView子視圖)的顯示;
//子視圖默認座標(0,0),大小是UIScrollView的大小;
UIScrollView *scrollView = [[UIScrollView alloc]
initWithFrame:self.view.bounds];
//2.顯示在界面上
[self.view addSubview:scrollView];
scrollView.tag = 101;
//3.設置背景顏色
scrollView.backgroundColor = [UIColor redColor];
//4.在scrollView上顯示一張圖片
UIImage *image = [UIImage imageNamed:@"1092.jpg"];
UIImageView *imageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageView.image = image;
imageView.tag = 100;
[scrollView addSubview:imageView];
//============scrollView核心屬性================================
//5.讓scrollView滾動起來的前提
//scrollView的內容視圖的大小大於scrollView的frame的大小
//內容視圖的寬度大於scrollView的寬度,滾動視圖能夠左右滾動
//內容視圖的高度大於scrollView的高度,滾動視圖能夠上下滾動
//讓scrollView滾動起來就是改變內容視圖的大小
scrollView.contentSize = CGSizeMake(image.size.width,image.size.height);
//6.設置偏移量
scrollView.contentOffset = CGPointMake(0, 600);
// [scrollView setContentOffset:CGPointMake(500, 500) animated:YES];
//!!!設置代理
scrollView.delegate = self;
//==================其餘屬性====================
//7.contentInset
//設置content視圖到scrollView上左下右的邊距;
//默認是(0,0,0,0)
[scrollView setContentInset:UIEdgeInsetsMake(20, 20, 20, 20)];
//8.pagingEnabled是否能夠翻頁
//每次滾動都是滾動一頁;(若是是左右滾動
//就是滾動scrollView的寬度)
//若是上下滾動就是滾動scrollView高度;
scrollView.pagingEnabled = YES;
//9.設置是否能夠滾動
[scrollView setScrollEnabled:YES];
//10.設置是否顯示滾動條
scrollView.showsVerticalScrollIndicator = YES;
scrollView.showsHorizontalScrollIndicator = NO;
//11.設置滾動條的樣式
[scrollView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
//===============縮放相關====================
//想要對scrollView進行縮放,實質是對添加到
//scrollView內容進行縮放
//1.最小縮放比例(默認是1)
scrollView.minimumZoomScale = 0.5;
//2.最大縮放比例(默認是1)
scrollView.maximumZoomScale = 2;
}
#pragma mark - UIScrollView 的代理
//MARK:縮放相關
//返回值就是須要縮放的對象
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [scrollView viewWithTag:100];
}
//MARK:滾動相關
//scrollView滾動的時候實時調用這個方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//通常在這裏獲取scrollView的偏移量;
CGPoint point = scrollView.contentOffset;
NSLog(@":%@",NSStringFromCGPoint(point));
if (point.y <= 0) {
scrollView.contentOffset = CGPointMake(point.x
, 0);
}
}
//MARK:拖拽
//將要開始滾動的時候調用這個方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"拖拽");
}
//MARK:將要中止拖拽的時候(將要鬆手的時候)
//參數1:委託
//參數2:中止拖拽的時候x和y方向的加速度
//參數3:滾動中止的時候scrollView的偏移量;(中止滾動的時候的位置信息)
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)
targetContentOffset{
//NSLog(@"%@",);
}
//已經中止拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"中止拖拽");
}
//將要開始減速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"開始減速");
}
//至關於已經中止滾動的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"已經中止滾動~~~~~");
}
//已經中止滾動動畫
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//這個中止是經過[scrollView setContentOffset:animation]產生的動畫;
NSLog(@"中止動畫");
}
//設置點擊狀態欄是否能夠回到頂部,默認是yes
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
return YES;
}
@end