iOS高仿QQ侧滑效果

[转载]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
查看查看186 回复回复3 收藏收藏 分享淘帖 转播转播 分享分享 微信
查看: 186|回复: 3
收起左侧

[转载] iOS高仿QQ侧滑效果

[复制链接]
SwiftRobot 发表于 2016-10-27 08:24:49 | 显示全部楼层 |阅读模式
快来登录
获取优质的苹果资讯内容
收藏热门的iOS等技术干货
拷贝下载Swift Demo源代码
订阅梳理好了的知识点专辑

效果图如下:

iOS高仿QQ侧滑效果

iOS高仿QQ侧滑效果 - 敏捷大拇指 - iOS高仿QQ侧滑效果





前言

左滑重点不在动画效果的实现,而在于左边视图事件响应后,怎么push进入主视图的NavigationController,如果主视图是TabBar的话,要获取当前TabBar的selecteItem,并push进入对应的导航容器中,正文以最为典型的TabBar作为Main主视图。

更多高仿代码请订阅我在敏捷大拇指上的淘帖专辑《Swift 高仿App源代码库》。




实现步骤

1、首先实现工具类EVNSideSlideViewController,支持左滑和右滑,左视图-主视图-右视图,记得用的时候要将EVNSideSlideViewController做为你的根视图;

2、左右视图的设计及主视图控制器的设计,按照原来做;

3、使用单例类记录主视图TabBar的选择状态,以便于push到当前页面所在的NavigationController中;

4、左右视图单击事件处理,详情见,正文的第三部分。




1、工具类EVNSideSlideViewController实现

下面的BaseViewController可以改成UIViewController


EVNSideSlideViewController.h

[Objective-C] 纯文本查看 复制代码
//
//  EVNSideSlideViewController.h
//  MMB-Saler
//
//  Created by developer on 16/6/28.
//  Copyright © 2016年 仁伯安. All rights reserved.
//

#import "BaseViewController.h"

@interface EVNSideSlideViewController : BaseViewController
{
@private
    UIImageView * imgBackground;
    CGFloat scalef;
}

@property (strong, nonatomic) UIViewController *leftViewController;
@property (strong, nonatomic) UIViewController *mainViewController;
@property (strong, nonatomic) UIViewController *righViewController;

/**
 *  滑动速度系数,建议在[0.5, 1]之间,默认为0.5
 */
@property (assign, nonatomic) CGFloat sideSlideSpeed;

/**
 *  初始化,带有左右滑功能的视图
 *
 *  @param LeftViewController 左边设置的视图
 *  @param MainViewController 主视图,可以是TabBarController
 *  @param RighViewController 右滑显示的视图
 *  @param image              当前视图背景图
 *
 *  @return EVNSideSlideViewController object
 */
- (instancetype)initWithLeftViewController:(UIViewController *)LeftViewController andMainViewController:(UIViewController *)MainViewController andRightViewController:(UIViewController *)RighViewController andBackgroundImage:(UIImage *)image;

/**
 *  显示主视图控制器
 */
- (void)showMainViewController;

/**
 *  显示左视图控制器
 */
- (void)showLeftViewController;

/**
 *  显示右视图控制器
 */
- (void)showRighViewController;

@end



EVNSideSlideViewController.m

[Objective-C] 纯文本查看 复制代码
//
//  EVNSideSlideViewController.m
//  MMB-Saler
//
//  Created by developer on 16/6/28.
//  Copyright © 2016年 仁伯安. All rights reserved.
//
#import "EVNSideSlideViewController.h"

@interface EVNSideSlideViewController ()

@end

@implementation EVNSideSlideViewController

- (instancetype)initWithLeftViewController:(UIViewController *)LeftViewController andMainViewController:(UIViewController *)MainViewController andRightViewController:(UIViewController *)RighViewController andBackgroundImage:(UIImage *)image
{
     self = [super init];

    if(self)
    {
        _sideSlideSpeed = 0.5;

        _leftViewController = LeftViewController;
        _mainViewController = MainViewController;
        _righViewController = RighViewController;

        UIImageView * imgview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [imgview setImage:image];
        [self.view addSubview:imgview];

    }
     return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    /**
     * 添加滑动手势
     */
     UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    [_mainViewController.view addGestureRecognizer:panGes];

    _leftViewController.view.hidden = YES;
    _righViewController.view.hidden = YES;

    [self.view addSubview:_leftViewController.view];
    [self.view addSubview:_righViewController.view];
    [self.view addSubview:_mainViewController.view];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  滑动手势
 *
 *  @param panGestureRecognizer 滑动手势
 */
- (void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer
{

    CGPoint point = [panGestureRecognizer translationInView:self.view];
    scalef = (point.x*_sideSlideSpeed + scalef);
    /**
     *  根据视图位置判断是左滑还是右边滑动
     */
    if (panGestureRecognizer.view.frame.origin.x >= 0)
    {
        panGestureRecognizer.view.center = CGPointMake(panGestureRecognizer.view.center.x + point.x*_sideSlideSpeed,panGestureRecognizer.view.center.y);
         panGestureRecognizer.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - scalef/1000, 1 - scalef/1000);
        [panGestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];
        _righViewController.view.hidden = YES;
        _leftViewController.view.hidden = NO;
    }
     else
    {
         panGestureRecognizer.view.center = CGPointMake(panGestureRecognizer.view.center.x + point.x*_sideSlideSpeed,panGestureRecognizer.view.center.y);
         panGestureRecognizer.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + scalef/1000, 1 + scalef/1000);
        [panGestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];

        _righViewController.view.hidden = NO;
        _leftViewController.view.hidden = YES;
    }

    /**
     *  有效手势判断、并调整相应的视图
     */
     if (panGestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
         if (scalef > 120*_sideSlideSpeed)
        {
            [self showLeftViewController];
        }
         else if (scalef < - 120*_sideSlideSpeed)
        {
            [self showRighViewController];
        }
        else
        {
            [self showMainViewController];
            scalef = 0;
        }
    }
}

- (void)showMainViewController
{
    [UIView beginAnimations:nil context:nil];
    _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
    _mainViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2.f, [UIScreen mainScreen].bounds.size.height/2.f);
    [UIView commitAnimations];
}

- (void)showLeftViewController
{
    [UIView beginAnimations:nil context:nil];
    _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
    _mainViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width * 3/2.f - 88.f, [UIScreen mainScreen].bounds.size.height/2.f);
    [UIView commitAnimations];
}

- (void)showRighViewController
{
    [UIView beginAnimations:nil context:nil];
    _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
    _mainViewController.view.center = CGPointMake(-[UIScreen mainScreen].bounds.size.width/2.f + 88.f, [UIScreen mainScreen].bounds.size.height/2.f);
    [UIView commitAnimations];
}
@end





2、CommonHelper 单例记录TabBar的状态


CommonHelper.h

[Objective-C] 纯文本查看 复制代码
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CommonHelper : NSObject<NSCopying, NSMutableCopying>
@property (strong, nonatomic) UIViewController *rootViewController;

 + (instancetype)shareInstance;
- (void)setObjValue;

@end



CommonHelper.m

[Objective-C] 纯文本查看 复制代码
#import "CommonHelper.h"

 static id _instance;

@implementation CommonHelper


///**
// *  只要系统中引用了该类,程序运行,就会主动调用load(不用手动调用,而且只会加载1次)
// */
//+ (void)load
//{
//    _instance = [[CommonHelper alloc] init];
//}

 + (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

 + (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

- (id)mutableCopyWithZone:(NSZone *)zone
{
    return _instance;
}

// 设置当前window的根视图
- (void)setObjValue
{
    [_instance setRootViewController:[[UIApplication sharedApplication] keyWindow].rootViewController];
}

@end





3、左、右视图点击事件触发写法

[Objective-C] 纯文本查看 复制代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonHelper *common = [CommonHelper shareInstance];
    [common setObjValue];

    NSLog(@"%@", NSStringFromClass([common.rootViewController class]));
    EVNSideSlideViewController *sideSlideViewController = (EVNSideSlideViewController *)common.rootViewController;
    [sideSlideViewController showMainViewController];

//    NSLog(@"%@", NSStringFromClass(sideSlideViewController.mainViewController));

    BuyerTabbarCtrl *toolController = (BuyerTabbarCtrl *)sideSlideViewController.mainViewController;
    NSInteger selectIndex = toolController.selectedIndex;

//    NSLog(@"%@", NSStringFromClass(toolController.viewControllers[0]));

        // 关于
    AboutMaibeiViewController * aboutMaibeiViewController = [[AboutMaibeiViewController alloc] init];
    aboutMaibeiViewController.hidesBottomBarWhenPushed = YES;
    [toolController.viewControllers[selectIndex] pushViewController:aboutMaibeiViewController animated:YES];
}



更多高仿代码请订阅我在敏捷大拇指上的淘帖专辑《Swift 高仿App源代码库》。




作者:仁伯安

都看到这里了,就把这篇资料推荐给您的好朋友吧,让他们也感受一下。

回帖是一种美德,也是对楼主发帖的尊重和支持。

*声明:敏捷大拇指是全球最大的Swift开发者社区、苹果粉丝家园、智能移动门户,所载内容仅限于传递更多最新信息,并不意味赞同其观点或证实其描述;内容仅供参考,并非绝对正确的建议。本站不对上述信息的真实性、合法性、完整性做出保证;转载请注明来源并加上本站链接,敏捷大拇指将保留所有法律权益。如有疑问或建议,邮件至marketing@swifthumb.com

*联系:微信公众平台:“swifthumb” / 腾讯微博:@swifthumb / 新浪微博:@swifthumb / 官方QQ一群:343549891(满) / 官方QQ二群:245285613 ,需要报上用户名才会被同意进群,请先注册敏捷大拇指

嗯,不错!期待更多好内容,支持一把:
支持敏捷大拇指,用支付宝支付10.24元 支持敏捷大拇指,用微信支付10.24元

评分

参与人数 1金钱 +10 贡献 +10 专家分 +10 收起 理由
Anewczs + 10 + 10 + 10 32个赞!专家给力!

查看全部评分

本帖被以下淘专辑推荐:

我是90后 发表于 2016-10-27 23:50:01 | 显示全部楼层
不错。

订阅了你的淘帖专辑
苏格拉没有底 发表于 2016-11-1 01:58:20 | 显示全部楼层
我是90后 发表于 2016-10-27 23:50
不错。

订阅了你的淘帖专辑

我也去订阅一下。
firefighter 发表于 2016-11-16 16:24:28 | 显示全部楼层
效果还不错。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

做任务,领红包。
我要发帖

分享扩散

都看到这里了,就把这资料推荐给您的好朋友吧,让他们也感受一下。
您的每一位朋友访问此永久链接后,您都将获得相应的金钱积分奖励
热门推荐

合作伙伴

Swift小苹果

  • 北京治世天下科技有限公司
  • ©2014-2016 敏捷大拇指
  • 京ICP备14029482号
  • Powered by Discuz! X3.1 Licensed
  • swifthumb Wechat Code
  •   
快速回复 返回顶部 返回列表