简单实现下拉图片放大
iOS - 面试时有力的谈资 运行时
你值得拥有,大神勿喷
github地址点我
传送门 :
Untitled3.gif
- 简单实现下拉图片放大① - 全屏手势
简单实现下拉图片放大② - 单张图
简单实现下拉图片放大③ - 定时器轮播图
简单实现下拉图片放大④ + pageControl指示器
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改变
网友评论