工作變化.jpg
簡(jiǎn)單實(shí)現(xiàn)下拉圖片放大
iOS - 面試時(shí)有力的談資 運(yùn)行時(shí)
你值得擁有,大神勿噴
github地址點(diǎn)我
傳送門 :
- 簡(jiǎn)單實(shí)現(xiàn)下拉圖片放大① - 全屏手勢(shì)
簡(jiǎn)單實(shí)現(xiàn)下拉圖片放大② - 單張圖
簡(jiǎn)單實(shí)現(xiàn)下拉圖片放大③ - 定時(shí)器輪播圖
簡(jiǎn)單實(shí)現(xiàn)下拉圖片放大④ + pageControl指示器
Untitled3.gif
1.全屏手勢(shì)的實(shí)現(xiàn)
#import <objc/runtime.h>
- 運(yùn)行時(shí)關(guān)聯(lián)對(duì)象 添加屬性
UIPanGestureRecognizer交互手勢(shì)
//懶加載 初始化UIPanGestureRecognizer交互手勢(shì)
- (UIPanGestureRecognizer *)hm_popGestureRecognizer {
UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd);
if (panGestureRecognizer == nil) {
panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
panGestureRecognizer.maximumNumberOfTouches = 1;
//分類中使用 運(yùn)行時(shí)關(guān)聯(lián)對(duì)象 添加屬性
objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return panGestureRecognizer;
}
- 判斷是否手勢(shì)的觸發(fā)
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
//判斷根視圖的數(shù)量 是否可用使用手勢(shì)返回
if (self.navigationController.viewControllers.count <= 1) {
return NO;
}
//KVC判斷如果正在轉(zhuǎn)場(chǎng)動(dòng)畫 取消手勢(shì)
if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {
return NO;
}
//判斷手指移動(dòng)方向
CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
if (translation.x <= 0) {
return NO;
}
return YES;
}
- swizzledMethod (黑魔法)
使用運(yùn)行時(shí)的交換方法 替換系統(tǒng)的 pushViewController:animated:方法
+ (void)load {
Method originalMethod = class_getInstanceMethod([self class], @selector(pushViewController:animated:));
Method swizzledMethod = class_getInstanceMethod([self class], @selector(hm_pushViewController:animated:));
//替換系統(tǒng)的 pushViewController:animated:方法
method_exchangeImplementations(originalMethod, swizzledMethod);
}
- 實(shí)現(xiàn)自己的含手勢(shì)的pop方法
- (void)hm_pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
//在所有的交互手勢(shì)中查找 如果沒有就添加
if (![self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.hm_popGestureRecognizer]) {
[self.interactivePopGestureRecognizer.view addGestureRecognizer:self.hm_popGestureRecognizer];
NSArray *targets = [self.interactivePopGestureRecognizer valueForKey:@"targets"];
id internalTarget = [targets.firstObject valueForKey:@"target"];
//攔截所有的系統(tǒng)手勢(shì)方法 handleNavigationTransition
SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
self.hm_popGestureRecognizer.delegate = [self hm_fullScreenPopGestureRecognizerDelegate];
[self.hm_popGestureRecognizer addTarget:internalTarget action:internalAction];
// 禁用系統(tǒng)的交互手勢(shì)
self.interactivePopGestureRecognizer.enabled = NO;
}
if (![self.viewControllers containsObject:viewController]) {
[self hm_pushViewController:viewController animated:animated];
}
}
獲取下面內(nèi)容 請(qǐng)點(diǎn)擊上面鏈接
簡(jiǎn)單實(shí)現(xiàn)下拉圖片放大②
- 下拉放大
- 上滑圖片漸變消失
- 狀態(tài)欄的顏色根據(jù)圖片alpha改變