簡單實現下拉圖片放大① - 全屏手勢

工作變化.jpg

簡單實現下拉圖片放大
iOS - 面試時有力的談資 運行時
你值得擁有,大神勿噴

github地址點我

傳送門 :

Untitled3.gif

1.全屏手勢的實現

#import <objc/runtime.h>
  • 運行時關聯對象 添加屬性
    UIPanGestureRecognizer交互手勢
//懶加載 初始化UIPanGestureRecognizer交互手勢
- (UIPanGestureRecognizer *)hm_popGestureRecognizer {
    UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd);
    
    if (panGestureRecognizer == nil) {
        panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
        panGestureRecognizer.maximumNumberOfTouches = 1;
        //分類中使用 運行時關聯對象 添加屬性
        objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return panGestureRecognizer;
}
  • 判斷是否手勢的觸發
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    //判斷根視圖的數量 是否可用使用手勢返回
    if (self.navigationController.viewControllers.count <= 1) {
        return NO;
    }
    //KVC判斷如果正在轉場動畫 取消手勢
    if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {
        return NO;
    }
    //判斷手指移動方向
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    if (translation.x <= 0) {
        return NO;
    }
    
    return YES;
}
  • swizzledMethod (黑魔法)

使用運行時的交換方法 替換系統的 pushViewController:animated:方法

+ (void)load {
    
    Method originalMethod = class_getInstanceMethod([self class], @selector(pushViewController:animated:));
    Method swizzledMethod = class_getInstanceMethod([self class], @selector(hm_pushViewController:animated:));
    //替換系統的 pushViewController:animated:方法
    method_exchangeImplementations(originalMethod, swizzledMethod);
}
  • 實現自己的含手勢的pop方法
- (void)hm_pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    //在所有的交互手勢中查找 如果沒有就添加
    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"];
        //攔截所有的系統手勢方法 handleNavigationTransition
        SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
        
        self.hm_popGestureRecognizer.delegate = [self hm_fullScreenPopGestureRecognizerDelegate];
        [self.hm_popGestureRecognizer addTarget:internalTarget action:internalAction];
        
        // 禁用系統的交互手勢
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    if (![self.viewControllers containsObject:viewController]) {
        [self hm_pushViewController:viewController animated:animated];
    }
}

獲取下面內容 請點擊上面鏈接
簡單實現下拉圖片放大②

  • 下拉放大
  • 上滑圖片漸變消失
  • 狀態欄的顏色根據圖片alpha改變
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容