IOS 開發(fā)之查看大圖的實(shí)現(xiàn)代碼
IOS 開發(fā)之查看大圖的實(shí)現(xiàn)代碼
本項(xiàng)目是取自傳智播客的教學(xué)項(xiàng)目,加入筆者的修改和潤(rùn)飾。
1. 項(xiàng)目名稱:查看大圖
2. 項(xiàng)目截圖展示

3. 項(xiàng)目功能
- 左右滑動(dòng)查看圖片
- 支持縮放功能
- 點(diǎn)擊中間按鈕移動(dòng)圖片
4. 項(xiàng)目代碼
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 設(shè)置內(nèi)容尺寸
self.scrollView.contentSize = self.imageView.frame.size;
// 設(shè)置
self.scrollView.delegate = self;
// 設(shè)置最大和最小的縮放比例
self.scrollView.maximumZoomScale = 2.0;
self.scrollView.minimumZoomScale = 0.2;
// 設(shè)置邊距
self.scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
// 不顯示水平滾動(dòng)標(biāo)示
self.scrollView.showsHorizontalScrollIndicator = NO;
// 不顯示垂直滾動(dòng)標(biāo)示
self.scrollView.showsVerticalScrollIndicator = NO;
// 偏移位置
self.scrollView.contentOffset = CGPointMake(0, -100);
// 取消彈簧效果
self.scrollView.bounces = NO;
//設(shè)置按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn.center = self.view.center;
[self.view addSubview:btn];
//設(shè)置按鈕的監(jiān)聽方法
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
// 移動(dòng)大圖的偏移位置
- (void)click
{
//取出offset
CGPoint offset = self.scrollView.contentOffset;
offset.x += 20;
offset.y += 20;
// 更新contentOffset
self.scrollView.contentOffset = offset;
}
#pragma mark - UIScrollView的代理方法
// 1> scrollView要知道縮放誰
/**
* 當(dāng)用戶開始拖拽scrollView時(shí)就會(huì)調(diào)用
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"開始拖拽");
}
/**
* 只要scrollView正在滾動(dòng),就會(huì)調(diào)用
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"正在滾動(dòng)%@", NSStringFromCGPoint(scrollView.contentOffset));
}
/**
* 當(dāng)用戶使用捏合手勢(shì)的時(shí)候會(huì)調(diào)用
*
* @return 返回的控件就是需要進(jìn)行縮放的控件
*/
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
NSLog(@"開始縮放");
return self.imageView;
}
/**
* 正在縮放的時(shí)候會(huì)調(diào)用
*/
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
NSLog(@"正在縮放");
}
@end
5. 本項(xiàng)目必須掌握的代碼段
設(shè)置外邊距
self.scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
結(jié)合類型創(chuàng)建按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
移動(dòng)scroll內(nèi)容的offset
- (void)click
{
CGPoint offset = self.scrollView.contentOffset;
offset.x += 20;
offset.y += 20;
self.scrollView.contentOffset = offset;
}
6. 筆記
scrollView無法滾動(dòng)的原因:
- 沒有設(shè)置contentSize
- scrollEnabled = NO
- 沒有接收到觸摸事件:userInteractionEnabled = NO
- 沒有取消autolayout功能(如果在Storyboard中添加了ScrollView的子控件,要想scrollView滾動(dòng),必須取消autolayout)
scrollView的屬性
@property(nonatomic) UIEdgeInsets contentInset;
這個(gè)屬性能夠在UIScrollView的4周增加額外的滾動(dòng)區(qū)域 @property(nonatomic) CGPoint contentOffset; 這個(gè)屬性用來表示UIScrollView滾動(dòng)的位置 @property(nonatomic) CGSize contentSize; 這個(gè)屬性用來表示UIScrollView內(nèi)容的尺寸,滾動(dòng)范圍(能滾多遠(yuǎn)) @property(nonatomic) BOOL bounces; 設(shè)置UIScrollView是否需要彈簧效果 @property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled; 設(shè)置UIScrollView是否能滾動(dòng) @property(nonatomic) BOOL showsHorizontalScrollIndicator; 是否顯示水平滾動(dòng)條 @property(nonatomic) BOOL showsVerticalScrollIndicator; 是否顯示垂直滾動(dòng)條
什么時(shí)候需要scrollView的代理?
當(dāng)我們想在UIScrollView正在滾動(dòng) 或 滾動(dòng)到某個(gè)位置 或者 停止?jié)L動(dòng) 時(shí)做一些特定的操作的時(shí)候,我們需要能夠監(jiān)聽到UIScrollView的整個(gè)滾動(dòng)過程。
也就是說,要想監(jiān)聽UIScrollView的滾動(dòng)過程,就必須先給UIScrollView設(shè)置一個(gè)代理對(duì)象(控制器),然后通過代理得知UIScrollView的滾動(dòng)過程。
UIScrollView將delegate需要實(shí)現(xiàn)的方法(監(jiān)聽scrollView的方法)都定義在了UIScrollViewDelegate協(xié)議中,因此要想成為UIScrollView的delegate,必須遵守UIScrollViewDelegate協(xié)議,然后實(shí)現(xiàn)協(xié)議中相應(yīng)的方法,就可以監(jiān)聽UIScrollView的滾動(dòng)過程了。
一般情況下,就設(shè)置UIScrollView所在的控制器 為 UIScrollView的delegate。
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
VIVO手機(jī)上del鍵無效OnKeyListener不響應(yīng)的原因及解決方法
最近有用戶反饋VIVO手機(jī)上回出現(xiàn),Del鍵無效的問題,最后找到問題所在是EdiText的OnKeyListener沒有響應(yīng),下面通過本文給大家分享下解決方案2016-12-12
在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析
這篇文章主要介紹了在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析,包括獲取當(dāng)前位置和計(jì)算兩點(diǎn)間距離等基本功能的實(shí)現(xiàn),需要的朋友可以參考下2016-05-05
ios12中遇到的帶input彈窗的錯(cuò)位問題的解決方法
這篇文章主要介紹了ios12中遇到的帶input彈窗的錯(cuò)位問題的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05

