一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android仿新版微信浮窗效果

Android仿新版微信浮窗效果

2022-03-03 15:01juejin Android

在新版微信中,可以把瀏覽的文章縮小為浮窗.點擊浮窗繼續閱讀.這篇文章主要介紹了Android仿新版微信浮窗效果,需要的朋友可以參考下

閱讀公眾號或其他文章,經常需要暫時退出文章.

在新版微信中,可以把瀏覽的文章縮小為浮窗.點擊浮窗繼續閱讀.對于經常在微信里閱讀的人來說,這簡直就是人類之光.

微信效果如下

Android仿新版微信浮窗效果

微信效果

對于這功能我進行了仿寫.

效果如下

Android仿新版微信浮窗效果

仿寫效果

微信的大佬一定用了了不起的技術,我這里只是實現效果.

簡單寫了一個庫,一句代碼即可實現效果

github.com/SherlockQi/…

?
1
2
//在AppDelegate中將類名傳入即可
[HKFloatManager addFloatVcs:@[@"HKSecondViewController"]];

使用到的技術點

監聽側滑返回

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//設置邊緣側滑代理
self.navigationController.interactivePopGestureRecognizer.delegate = self;
 
//當開始側滑pop時調用此方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
 /* 判斷是否開啟邊緣側滑返回 **/
 if (self.navigationController.viewControllers.count > 1) {
   [self beginScreenEdgePanBack:gestureRecognizer];
  return YES;
 }
 return NO;
}
/* UIScreenEdgePanGestureRecognizer
@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/*! This subclass of UIPanGestureRecognizer only recognizes if the user slides their finger
 in from the bezel on the specified edge. */
//NS_CLASS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED @interface UIScreenEdgePanGestureRecognizer : UIPanGestureRecognizer
**/
//利用CADisplayLink 來實現監聽返回手勢
- (void)beginScreenEdgePanBack:(UIGestureRecognizer *)gestureRecognizer{
   /*
   * 引用 gestureRecognizer
   * 開啟 CADisplayLink
   * 顯示右下視圖
   **/
 self.edgePan = (UIScreenEdgePanGestureRecognizer *)gestureRecognizer;
 _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(panBack:)];
 [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
 [[UIApplication sharedApplication].keyWindow addSubview:self.floatArea];
}
//此方法中進行操作
- (void)panBack:(CADisplayLink *)link {
 //判斷手勢狀態
 if (self.edgePan.state == UIGestureRecognizerStateChanged) {//移動過程
  /*
  * 改變右下視圖 frame
  * 判斷手指是否進入右下視圖中
  **/
 //手指在屏幕上的位置
 CGPoint tPoint = [self.edgePan translationInView:kWindow];
  ...根據tPoint設置右下視圖 frame...
 //手指在右下視圖上的位置(若 x>0 && y>0 說明此時手指在右下視圖上)
 CGPoint touchPoint = [kWindow convertPoint:[self.edgePan locationInView:kWindow] toView:self.floatArea];
 if (touchPoint.x > 0 && touchPoint.y > 0) {
    ...
    //由于右下視圖是1/4圓 所以需要這步判斷
    if (pow((kFloatAreaR - touchPoint.x), 2) + pow((kFloatAreaR - touchPoint.y), 2) <= pow((kFloatAreaR), 2)) {
     self.showFloatBall = YES;
    }
    ...
 }else if (self.edgePan.state == UIGestureRecognizerStatePossible) {
  /*
  * 停止CADisplayLink
  * 隱藏右下視圖
  * 顯示/隱藏浮窗
  **/
  [self.link invalidate];
  if (self.showFloatBall) { 
    self.floatBall.iconImageView.image= [self.floatViewController valueForKey:@"hk_iconImage"];
    [kWindow addSubview:self.floatBall];
   }
 }
}

監聽浮窗移動/點擊

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#import "HKFloatBall.h" 類為浮窗視圖類
//點擊浮窗后讓代理push之前保留起來的控制器
- (void)tap:(UIGestureRecognizer *)tap{
 if ([self.delegate respondsToSelector:@selector(floatBallDidClick:)]) {
  [self.delegate floatBallDidClick:self];
  }
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
 ...結束監聽...
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
 ...結束監聽...
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
 /*
 * 改變浮窗 frame
 * 改變右下視圖 frame
 * 判斷浮窗center 是否在右下視圖之上
 **/
 CGPoint center_ball = [kWindow convertPoint:self.floatBall.center toView:self.cancelFloatArea];
 if (pow((kFloatAreaR - center_ball.x), 2) + pow((kFloatAreaR - center_ball.y), 2) <= pow((kFloatAreaR), 2)) {
  if (!self.cancelFloatArea.highlight) {
   self.cancelFloatArea.highlight = YES;
  }
 }
}
}

自定義push/pop動畫

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//設置navigationController代理
 self.navigationController.delegate = self;
#pragma UINavigationControllerDelegate
//push/pop 時會調用此代理方法
- (nullable id )navigationController:(UINavigationController *)navigationController
           animationControllerForOperation:(UINavigationControllerOperation)operation
               fromViewController:(UIViewController *)fromVC
               toViewController:(UIViewController *)toVC{
 ... 判斷是否執行動畫 若 return nil 則執行原始 push/pop 動畫...
 //HKTransitionPush HKTransitionPop 是自己寫的兩個動畫類,需要實現 if(operation==UINavigationControllerOperationPush) {
  return [[HKTransitionPush alloc]init];
 } else if(operation==UINavigationControllerOperationPop){
  return [[HKTransitionPop alloc]init];
 }
}
HKTransitionPush HKTransitionPop 代碼類似已HKTransitionPush為例
#import "HKTransitionPush.h"
-(NSTimeInterval)transitionDuration:(id)transitionContext{
 return kAuration;//動畫時間
}
- (void)animateTransition:(id)transitionContext {
 //獲取上下文
 self.transitionContext = transitionContext;
 UIViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
 UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
 UIView *contView = [transitionContext containerView];
 [contView addSubview:fromVC.view];
 [contView addSubview:toVC.view];
 //添加遮罩視圖
 [fromVC.view addSubview:self.coverView];
 //浮窗的 frame push時這個是起始 frame ,pop時是結束時的 frame
 CGRect floatBallRect = [HKFloatManager shared].floatBall.frame;
 //開始/結束時的曲線
 UIBezierPath *maskStartBP = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(floatBallRect.origin.x, floatBallRect.origin.y,floatBallRect.size.width , floatBallRect.size.height) cornerRadius:floatBallRect.size.height/2];
 UIBezierPath *maskFinalBP = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT) cornerRadius:floatBallRect.size.width/2];
 //.layer.mask 是部分顯示的原因
 CAShapeLayer *maskLayer = [CAShapeLayer layer];
 maskLayer.path = maskFinalBP.CGPath;
 toVC.view.layer.mask = maskLayer;
 //動畫類
 CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
 maskLayerAnimation.fromValue = (__bridge id)(maskStartBP.CGPath);
 maskLayerAnimation.toValue = (__bridge id)((maskFinalBP.CGPath));
 maskLayerAnimation.duration = kAuration;
 maskLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 maskLayerAnimation.delegate = self;
 [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
 //隱藏浮窗
 [UIView animateWithDuration:kAuration animations:^{
  [HKFloatManager shared].floatBall.alpha = 0;
 }];
}
#pragma mark - CABasicAnimation的Delegate
//動畫完成后代理
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
 [self.transitionContext completeTransition:YES];
 [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
 [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;
 [self.coverView removeFromSuperview];
  
}
-(UIView *)coverView{
 if (!_coverView) {
  _coverView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
  _coverView.backgroundColor = [UIColor blackColor];
  _coverView.alpha = 0.5;
 };
 return _coverView;
}

解耦

將所有代碼集中在 #import "HKFloatManager.h" 中

?
1
2
//在AppDelegate中將類名傳入即可,在該類控制器側滑返回時啟動浮窗功能(需要在實例化導航控制器之后)
[HKFloatManager addFloatVcs:@[@"HKSecondViewController"]];

若需要設置浮窗頭像,設置該控制器的"hk_iconImage"

?
1
2
@property (nonatomic, strong) UIImage *hk_iconImage;
Tips

震動反饋

?
1
2
3
4
5
UIImpactFeedbackGenerator*impactLight = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleMedium];
[impactLight impactOccurred];
 // UIImpactFeedbackStyleLight,
 // UIImpactFeedbackStyleMedium,
 // UIImpactFeedbackStyleHeavy

分類獲取當前控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import "NSObject+hkvc.h"
@implementation NSObject (hkvc)
- (UIViewController *)hk_currentViewController
{
 UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
 UIViewController *vc = keyWindow.rootViewController;
  if ([vc isKindOfClass:[UINavigationController class]])
  {
   vc = [(UINavigationController *)vc visibleViewController];
  }
  else if ([vc isKindOfClass:[UITabBarController class]])
  {
   vc = [(UITabBarController *)vc selectedViewController];
  }
 return vc;
}
- (UINavigationController *)hk_currentNavigationController
{
 return [self hk_currentViewController].navigationController;
}
- (UITabBarController *)hk_currentTabBarController
{
 return [self hk_currentViewController].tabBarController;
}
@end

判斷控制器是否有"hk_iconImage"屬性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (BOOL)haveIconImage{
 BOOL have = NO;
 unsigned int outCount = 0;
 Ivar *ivars = class_copyIvarList([self.floatViewController class], &outCount);
 for (unsigned int i = 0; i < outCount; i ++) {
  Ivar ivar = ivars[i];
  const char * nameChar = ivar_getName(ivar);
  NSString *nameStr =[NSString stringWithFormat:@"%s",nameChar];
  if([nameStr isEqualToString:@"_hk_iconImage"]) {
   have = YES;
  }
 }
 free(ivars);
 return have;
}

以上便是實現該效果的全部實現.上方含有部分偽代碼.全部代碼已上傳至---Github--- 歡迎(跪求) Star.

以上所述是小編給大家介紹的Android仿新版微信浮窗效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://juejin.im/entry/5b2a05876fb9a00e2d47f725

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成年人在线视频观看 | 91一个人的在线观看www | 国产免费视 | 欧美精品国产第一区二区 | 和日本免费不卡在线v | 潘金莲西门庆一级淫片aaaaaa | 精品久久久噜噜噜久久7 | 日韩成人小视频 | 婷综合 | 91精品国产高清久久久久久 | 亚洲ss| 午夜在线观看免费观看 视频 | 天天色综合久久 | 国产日韩欧美不卡www | 久草在线福利资站免费视频 | 日本xxxxx69hd日本 | 男人女人日皮 | 亚洲欧美专区精品久久 | 精品女同一区二区三区免费站 | 亚洲天堂2016 | 好男人在线观看免费高清2019韩剧 | 天堂网在线.www天堂在线视频 | 亚洲第一成年免费网站 | 国产亚洲自愉自愉 | 慢慢娇淫| 色综合天天五月色 | 1024亚洲天堂| 肥胖女性大bbbbbb视频女厕 | 校园春色自拍偷拍 | 午夜无码片在线观看影院 | 精品午夜久久福利大片免费 | 国产成人精品一区二三区 | 免费一级日本c片完整版 | 国产成人在线播放视频 | 亚洲第一网色综合久久 | 青青青国产精品国产精品美女 | 草莓视频丝瓜 | www.四虎影| 男人女人日皮 | 极品主播的慰在线播放 | 无码AV免费精品一区二区三区 |