Android仿新版微信浮窗效果
閱讀公眾號或其他文章,經(jīng)常需要暫時退出文章.
在新版微信中,可以把瀏覽的文章縮小為浮窗.點(diǎn)擊浮窗繼續(xù)閱讀.對于經(jīng)常在微信里閱讀的人來說,這簡直就是人類之光.
微信效果如下

微信效果
對于這功能我進(jìn)行了仿寫.
效果如下

仿寫效果
微信的大佬一定用了了不起的技術(shù),我這里只是實現(xiàn)效果.
簡單寫了一個庫,一句代碼即可實現(xiàn)效果
//在AppDelegate中將類名傳入即可 [HKFloatManager addFloatVcs:@[@"HKSecondViewController"]];
使用到的技術(shù)點(diǎn)
監(jiān)聽側(cè)滑返回
//設(shè)置邊緣側(cè)滑代理
self.navigationController.interactivePopGestureRecognizer.delegate = self;
//當(dāng)開始側(cè)滑pop時調(diào)用此方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
/* 判斷是否開啟邊緣側(cè)滑返回 **/
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 來實現(xiàn)監(jiān)聽返回手勢
- (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];
}
//此方法中進(jìn)行操作
- (void)panBack:(CADisplayLink *)link {
//判斷手勢狀態(tài)
if (self.edgePan.state == UIGestureRecognizerStateChanged) {//移動過程
/*
* 改變右下視圖 frame
* 判斷手指是否進(jìn)入右下視圖中
**/
//手指在屏幕上的位置
CGPoint tPoint = [self.edgePan translationInView:kWindow];
...根據(jù)tPoint設(shè)置右下視圖 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];
}
}
}
監(jiān)聽浮窗移動/點(diǎn)擊
#import "HKFloatBall.h" 類為浮窗視圖類
//點(diǎn)擊浮窗后讓代理push之前保留起來的控制器
- (void)tap:(UIGestureRecognizer *)tap{
if ([self.delegate respondsToSelector:@selector(floatBallDidClick:)]) {
[self.delegate floatBallDidClick:self];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
...結(jié)束監(jiān)聽...
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
...結(jié)束監(jiān)聽...
}
- (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動畫
//設(shè)置navigationController代理
self.navigationController.delegate = self;
#pragma UINavigationControllerDelegate
//push/pop 時會調(diào)用此代理方法
- (nullable id )navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC{
... 判斷是否執(zhí)行動畫 若 return nil 則執(zhí)行原始 push/pop 動畫...
//HKTransitionPush HKTransitionPop 是自己寫的兩個動畫類,需要實現(xiàn) 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時是結(jié)束時的 frame
CGRect floatBallRect = [HKFloatManager shared].floatBall.frame;
//開始/結(jié)束時的曲線
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" 中
//在AppDelegate中將類名傳入即可,在該類控制器側(cè)滑返回時啟動浮窗功能(需要在實例化導(dǎo)航控制器之后) [HKFloatManager addFloatVcs:@[@"HKSecondViewController"]];
若需要設(shè)置浮窗頭像,設(shè)置該控制器的"hk_iconImage"
@property (nonatomic, strong) UIImage *hk_iconImage; Tips
震動反饋
UIImpactFeedbackGenerator*impactLight = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleMedium]; [impactLight impactOccurred]; // UIImpactFeedbackStyleLight, // UIImpactFeedbackStyleMedium, // UIImpactFeedbackStyleHeavy
分類獲取當(dāng)前控制器
#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"屬性
- (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;
}
以上便是實現(xiàn)該效果的全部實現(xiàn).上方含有部分偽代碼.全部代碼已上傳至---Github--- 歡迎(跪求) Star.
以上所述是小編給大家介紹的Android仿新版微信浮窗效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解Android 通過Socket 和服務(wù)器通訊(附demo)
Android 通過Socket 和服務(wù)器通訊,是一種比較常用的通訊方式,這篇文章主要介紹了詳解Android 通過Socket 和服務(wù)器通訊,有興趣的可以了解一下。2016-12-12
Android實現(xiàn)Gesture手勢識別用法分析
這篇文章主要介紹了Android實現(xiàn)Gesture手勢識別用法,結(jié)合實例形式較為詳細(xì)的分析了Android基于Gesture實現(xiàn)手勢識別的原理與具體實現(xiàn)技巧,需要的朋友可以參考下2016-09-09
Android中AsyncTask異步任務(wù)使用詳細(xì)實例(一)
AsyncTask是Android提供的輕量級的異步類,可以直接繼承AsyncTask,在類中實現(xiàn)異步操作,并提供接口反饋當(dāng)前異步執(zhí)行的程度(可以通過接口實現(xiàn)UI進(jìn)度更新),最后反饋執(zhí)行的結(jié)果給UI主線程,通過本文給大家介紹Android中AsyncTask異步任務(wù)使用詳細(xì)實例(一),需要的朋友參考下2016-02-02
flutter?material?widget組件之信息展示組件使用詳解
這篇文章主要為大家詳細(xì)介紹了flutter?material?widget組件之信息展示組件的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
Android自定義view實現(xiàn)水波紋進(jìn)度球效果
在我們的日常開發(fā)中自定義控件還是用的挺多的,設(shè)計師或者產(chǎn)品為了更好的漂亮,美觀,交互都會做一些牛逼的ui效果圖,但是最后實現(xiàn)的還是我們程序員啊。所以說 自定義view你還是得會的。2016-08-08
Android LinearLayout實現(xiàn)自動換行
這篇文章主要為大家詳細(xì)介紹了Android LinearLayout實現(xiàn)自動換行,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android onTouchEvent事件中onTouch方法返回值(介紹)
下面小編就為大家?guī)硪黄狝ndroid onTouchEvent事件中onTouch方法返回值(介紹)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Android實現(xiàn)讓圖片在屏幕上任意移動的方法(拖拽功能)
這篇文章主要介紹了Android實現(xiàn)讓圖片在屏幕上任意移動的方法,實例分析了Android拖拽功能的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-08-08

