1、MJPopupBackgroundView

  • 1.1 MJPopupBackgroundView.h

    //
    // MJPopupBackgroundView.h
    // watched
    //
    // Created by Martin Juhasz on 18.06.12.
    // Copyright (c) 2012 martinjuhasz.de. All rights reserved.
    // #import <UIKit/UIKit.h> @interface MJPopupBackgroundView : UIView @end
  • 1.2 MJPopupBackgroundView.h

    //
    // MJPopupBackgroundView.m
    // watched
    //
    // Created by Martin Juhasz on 18.06.12.
    // Copyright (c) 2012 martinjuhasz.de. All rights reserved.
    // #import "MJPopupBackgroundView.h" @implementation MJPopupBackgroundView - (void)drawRect:(CGRect)rect
    {
    CGContextRef context = UIGraphicsGetCurrentContext();
    size_t locationsCount = 2;
    CGFloat locations[2] = {0.0f, 1.0f};
    CGFloat colors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.75f};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, locations, locationsCount);
    CGColorSpaceRelease(colorSpace); CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    float radius = MIN(self.bounds.size.width , self.bounds.size.height) ;
    CGContextDrawRadialGradient (context, gradient, center, 0, center, radius, kCGGradientDrawsAfterEndLocation);
    CGGradientRelease(gradient);
    } @end

2、UIViewController+MJPopupViewController

  • 2.1 UIViewController+MJPopupViewController.h

    //
    // UIViewController+MJPopupViewController.h
    // MJModalViewController
    //
    // Created by Martin Juhasz on 11.05.12.
    // Copyright (c) 2012 martinjuhasz.de. All rights reserved.
    // #import <UIKit/UIKit.h> @class MJPopupBackgroundView; typedef enum {
    MJPopupViewAnimationFade = 0,
    MJPopupViewAnimationSlideBottomTop = 1,
    MJPopupViewAnimationSlideBottomBottom,
    MJPopupViewAnimationSlideTopTop,
    MJPopupViewAnimationSlideTopBottom,
    MJPopupViewAnimationSlideLeftLeft,
    MJPopupViewAnimationSlideLeftRight,
    MJPopupViewAnimationSlideRightLeft,
    MJPopupViewAnimationSlideRightRight,
    } MJPopupViewAnimation; @interface UIViewController (MJPopupViewController) @property (nonatomic, retain) UIViewController *mj_popupViewController;
    @property (nonatomic, retain) MJPopupBackgroundView *mj_popupBackgroundView; - (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType;
    - (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType dismissed:(void(^)(void))dismissed;
    - (void)dismissPopupViewControllerWithanimationType:(MJPopupViewAnimation)animationType; @end
  • 2.1 UIViewController+MJPopupViewController.m

    //
    // UIViewController+MJPopupViewController.m
    // MJModalViewController
    //
    // Created by Martin Juhasz on 11.05.12.
    // Copyright (c) 2012 martinjuhasz.de. All rights reserved.
    // #import "UIViewController+MJPopupViewController.h"
    #import <QuartzCore/QuartzCore.h>
    #import "MJPopupBackgroundView.h"
    #import <objc/runtime.h> #define kPopupModalAnimationDuration 0.35
    #define kMJPopupViewController @"kMJPopupViewController"
    #define kMJPopupBackgroundView @"kMJPopupBackgroundView"
    #define kMJSourceViewTag 23941
    #define kMJPopupViewTag 23942
    #define kMJOverlayViewTag 23945 @interface UIViewController (MJPopupViewControllerPrivate) - (UIView*)topView;
    - (void)presentPopupView:(UIView*)popupView;
    @end static NSString *MJPopupViewDismissedKey = @"MJPopupViewDismissed";
    static BOOL boards = NO;
    static BOOL MJPopupViewisShow = NO;
    ////////////////////////////////////////////////////////////////////////////
    #pragma mark -
    #pragma mark Public @implementation UIViewController (MJPopupViewController) static void * const keypath = (void*)&keypath; - (UIViewController*)mj_popupViewController {
    return objc_getAssociatedObject(self, kMJPopupViewController);
    } - (void)setMj_popupViewController:(UIViewController *)mj_popupViewController {
    objc_setAssociatedObject(self, kMJPopupViewController, mj_popupViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (MJPopupBackgroundView*)mj_popupBackgroundView {
    return objc_getAssociatedObject(self, kMJPopupBackgroundView);
    } - (void)setMj_popupBackgroundView:(MJPopupBackgroundView *)mj_popupBackgroundView {
    objc_setAssociatedObject(self, kMJPopupBackgroundView, mj_popupBackgroundView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType dismissed:(void(^)(void))dismissed
    {
    if (!MJPopupViewisShow) {
    MJPopupViewisShow = YES;
    self.mj_popupViewController = popupViewController;
    [self presentPopupView:popupViewController.view animationType:animationType dismissed:dismissed];
    }
    } - (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType
    {
    [self presentPopupViewController:popupViewController animationType:animationType dismissed:nil];
    } - (void)dismissPopupViewControllerWithanimationType:(MJPopupViewAnimation)animationType
    {
    MJPopupViewisShow = NO;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MJPopupViewDismiss" object:nil];
    UIView *sourceView = [self topView];
    if (sourceView.tag != kMJSourceViewTag && sourceView.tag == kMJPopupViewTag) {
    sourceView = [sourceView superview];
    }
    UIView *popupView = [sourceView viewWithTag:kMJPopupViewTag];
    UIView *overlayView = [sourceView viewWithTag:kMJOverlayViewTag]; switch (animationType) {
    case MJPopupViewAnimationSlideBottomTop:
    case MJPopupViewAnimationSlideBottomBottom:
    case MJPopupViewAnimationSlideTopTop:
    case MJPopupViewAnimationSlideTopBottom:
    case MJPopupViewAnimationSlideLeftLeft:
    case MJPopupViewAnimationSlideLeftRight:
    case MJPopupViewAnimationSlideRightLeft:
    case MJPopupViewAnimationSlideRightRight:
    [self slideViewOut:popupView sourceView:sourceView overlayView:overlayView withAnimationType:animationType];
    break; default:
    [self fadeViewOut:popupView sourceView:sourceView overlayView:overlayView];
    break;
    }
    } ////////////////////////////////////////////////////////////////////////////
    #pragma mark -
    #pragma mark View Handling - (void)presentPopupView:(UIView*)popupView animationType:(MJPopupViewAnimation)animationType
    {
    if (!MJPopupViewisShow) {
    MJPopupViewisShow = YES;
    [self presentPopupView:popupView animationType:animationType dismissed:nil];
    }
    } - (void)keyboardAppearanced
    {
    boards = YES;
    } - (void)keyboardDismissed
    {
    boards = NO;
    } - (void)presentPopupView:(UIView*)popupView animationType:(MJPopupViewAnimation)animationType dismissed:(void(^)(void))dismissed
    {
    boards = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardAppearanced) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDismissed) name:UIKeyboardDidHideNotification object:nil]; UIView *sourceView = [self topView];
    sourceView.tag = kMJSourceViewTag;
    popupView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
    popupView.tag = kMJPopupViewTag; // check if source view controller is not in destination
    if ([sourceView.subviews containsObject:popupView]) return; // customize popupView
    popupView.layer.shadowPath = [UIBezierPath bezierPathWithRect:popupView.bounds].CGPath;
    popupView.layer.masksToBounds = YES;
    popupView.layer.shadowOffset = CGSizeMake(5, 5);
    popupView.layer.shadowRadius = 5;
    popupView.layer.shadowOpacity = 0.5;
    popupView.layer.cornerRadius = 5;
    popupView.layer.shouldRasterize = YES;
    popupView.layer.rasterizationScale = [[UIScreen mainScreen] scale]; // Add semi overlay
    UIView *overlayView = [[UIView alloc] initWithFrame:sourceView.bounds];
    overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    overlayView.tag = kMJOverlayViewTag;
    overlayView.backgroundColor = [UIColor clearColor]; // BackgroundView
    self.mj_popupBackgroundView = [[MJPopupBackgroundView alloc] initWithFrame:sourceView.bounds];
    self.mj_popupBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.mj_popupBackgroundView.backgroundColor = [UIColor clearColor];
    self.mj_popupBackgroundView.alpha = 0.0f;
    [overlayView addSubview:self.mj_popupBackgroundView]; // Make the Background Clickable
    UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    dismissButton.backgroundColor = [UIColor clearColor];
    dismissButton.frame = sourceView.bounds;
    [overlayView addSubview:dismissButton]; popupView.alpha = 0.0f;
    [overlayView addSubview:popupView];
    [sourceView addSubview:overlayView]; [dismissButton addTarget:self action:@selector(dismissPopupViewControllerWithanimation:) forControlEvents:UIControlEventTouchUpInside];
    switch (animationType) {
    case MJPopupViewAnimationSlideBottomTop:
    case MJPopupViewAnimationSlideBottomBottom:
    case MJPopupViewAnimationSlideTopTop:
    case MJPopupViewAnimationSlideTopBottom:
    case MJPopupViewAnimationSlideLeftLeft:
    case MJPopupViewAnimationSlideLeftRight:
    case MJPopupViewAnimationSlideRightLeft:
    case MJPopupViewAnimationSlideRightRight:
    dismissButton.tag = animationType;
    [self slideViewIn:popupView sourceView:sourceView overlayView:overlayView withAnimationType:animationType];
    break;
    default:
    dismissButton.tag = MJPopupViewAnimationFade;
    [self fadeViewIn:popupView sourceView:sourceView overlayView:overlayView];
    break;
    } [self setDismissedCallback:dismissed];
    } - (UIView*)topView {
    UIViewController *recentView = self; while (recentView.parentViewController != nil) {
    recentView = recentView.parentViewController;
    }
    return recentView.view;
    } - (void)dismissPopupViewControllerWithanimation:(id)sender
    {
    if (boards)
    {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"disMissKeyBoard" object:nil];
    return ;
    }
    if ([sender isKindOfClass:[UIButton class]]) {
    UIButton* dismissButton = sender;
    switch (dismissButton.tag) {
    case MJPopupViewAnimationSlideBottomTop:
    case MJPopupViewAnimationSlideBottomBottom:
    case MJPopupViewAnimationSlideTopTop:
    case MJPopupViewAnimationSlideTopBottom:
    case MJPopupViewAnimationSlideLeftLeft:
    case MJPopupViewAnimationSlideLeftRight:
    case MJPopupViewAnimationSlideRightLeft:
    case MJPopupViewAnimationSlideRightRight:
    [self dismissPopupViewControllerWithanimationType:(int)dismissButton.tag];
    break;
    default:
    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
    break;
    }
    } else {
    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
    }
    } //////////////////////////////////////////////////////////////////////////////
    #pragma mark -
    #pragma mark Animations #pragma mark --- Slide - (void)slideViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView withAnimationType:(MJPopupViewAnimation)animationType
    {
    // Generating Start and Stop Positions
    CGSize sourceSize = sourceView.bounds.size;
    CGSize popupSize = popupView.bounds.size;
    CGRect popupStartRect;
    switch (animationType) {
    case MJPopupViewAnimationSlideBottomTop:
    case MJPopupViewAnimationSlideBottomBottom:
    popupStartRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
    sourceSize.height,
    popupSize.width,
    popupSize.height); break;
    case MJPopupViewAnimationSlideLeftLeft:
    case MJPopupViewAnimationSlideLeftRight:
    popupStartRect = CGRectMake(-sourceSize.width,
    (sourceSize.height - popupSize.height) / 2,
    popupSize.width,
    popupSize.height);
    break; case MJPopupViewAnimationSlideTopTop:
    case MJPopupViewAnimationSlideTopBottom:
    popupStartRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
    -popupSize.height,
    popupSize.width,
    popupSize.height);
    break; default:
    popupStartRect = CGRectMake(sourceSize.width,
    (sourceSize.height - popupSize.height) / 2,
    popupSize.width,
    popupSize.height);
    break;
    }
    CGRect popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
    (sourceSize.height - popupSize.height) / 2,
    popupSize.width,
    popupSize.height); // Set starting properties
    popupView.frame = popupStartRect;
    popupView.alpha = 1.0f;
    [UIView animateWithDuration:kPopupModalAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
    [self.mj_popupViewController viewWillAppear:NO];
    self.mj_popupBackgroundView.alpha = 1.0f;
    popupView.frame = popupEndRect;
    } completion:^(BOOL finished) {
    [self.mj_popupViewController viewDidAppear:NO];
    }];
    } - (void)slideViewOut:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView withAnimationType:(MJPopupViewAnimation)animationType
    {
    // Generating Start and Stop Positions
    CGSize sourceSize = sourceView.bounds.size;
    CGSize popupSize = popupView.bounds.size;
    CGRect popupEndRect;
    switch (animationType) {
    case MJPopupViewAnimationSlideBottomTop:
    case MJPopupViewAnimationSlideTopTop:
    popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
    -popupSize.height,
    popupSize.width,
    popupSize.height);
    break;
    case MJPopupViewAnimationSlideBottomBottom:
    case MJPopupViewAnimationSlideTopBottom:
    popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
    sourceSize.height,
    popupSize.width,
    popupSize.height);
    break;
    case MJPopupViewAnimationSlideLeftRight:
    case MJPopupViewAnimationSlideRightRight:
    popupEndRect = CGRectMake(sourceSize.width,
    popupView.frame.origin.y,
    popupSize.width,
    popupSize.height);
    break;
    default:
    popupEndRect = CGRectMake(-popupSize.width,
    popupView.frame.origin.y,
    popupSize.width,
    popupSize.height);
    break;
    } [UIView animateWithDuration:kPopupModalAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
    [self.mj_popupViewController viewWillDisappear:NO];
    popupView.frame = popupEndRect;
    self.mj_popupBackgroundView.alpha = 0.0f;
    } completion:^(BOOL finished) {
    [popupView removeFromSuperview];
    [overlayView removeFromSuperview];
    [self.mj_popupViewController viewDidDisappear:NO];
    self.mj_popupViewController = nil; id dismissed = [self dismissedCallback];
    if (dismissed != nil)
    {
    ((void(^)(void))dismissed)();
    [self setDismissedCallback:nil];
    }
    }];
    } #pragma mark --- Fade
    - (void)fadeViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView
    {
    // Generating Start and Stop Positions
    CGSize sourceSize = sourceView.bounds.size;
    CGSize popupSize = popupView.bounds.size;
    CGRect popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
    (sourceSize.height - popupSize.height) / 2,
    popupSize.width,
    popupSize.height); // Set starting properties
    popupView.frame = popupEndRect;
    popupView.alpha = 0.0f; [UIView animateWithDuration:kPopupModalAnimationDuration animations:^{
    [self.mj_popupViewController viewWillAppear:NO];
    self.mj_popupBackgroundView.alpha = 0.5f;
    popupView.alpha = 1.0f;
    } completion:^(BOOL finished) {
    [self.mj_popupViewController viewDidAppear:NO];
    }];
    } - (void)fadeViewOut:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView
    {
    [UIView animateWithDuration:kPopupModalAnimationDuration animations:^{
    [self.mj_popupViewController viewWillDisappear:NO];
    self.mj_popupBackgroundView.alpha = 0.0f;
    popupView.alpha = 0.0f;
    } completion:^(BOOL finished) {
    [popupView removeFromSuperview];
    [overlayView removeFromSuperview];
    [self.mj_popupViewController viewDidDisappear:NO];
    self.mj_popupViewController = nil; id dismissed = [self dismissedCallback];
    if (dismissed != nil)
    {
    ((void(^)(void))dismissed)();
    [self setDismissedCallback:nil];
    }
    }];
    } #pragma mark - Category Accessors #pragma mark --- Dismissed - (void)setDismissedCallback:(void(^)(void))dismissed
    {
    objc_setAssociatedObject(self, &MJPopupViewDismissedKey, dismissed, OBJC_ASSOCIATION_RETAIN);
    } - (void(^)(void))dismissedCallback
    {
    return objc_getAssociatedObject(self, &MJPopupViewDismissedKey);
    } @end

UIViewController+MJPopupViewController的更多相关文章

  1. iOS 在类别里添加成员变量的方法:objc_setAssociatedObject

    今天在github上查看MJPopupViewController这个项目,发现里面用到了objc_setAssociatedObject,用来为类别添加成员变量. 我百度之后,发现有人是这样说明的: ...

  2. OS快速开发必备

    github:https://github.com/koknine (终于改成以前的了) 当前移动互联网行业太火爆,移动端的需求日益增长,很多开发人员每天都应对着各种需求,作为一名iOS开发人员,对于 ...

  3. 不准使用xib自定义控制器view的大小

    1.AppDelegate.m // // 文 件 名:AppDelegate.m // // 版权所有:Copyright © 2018年 leLight. All rights reserved. ...

  4. UIViewController生命周期-完整版

    一.UIViewController 的生命周期 下面带 (NSObject)的方法是NSObject提供的方法.其他的都是UIViewController 提供的方法. load   (NSObje ...

  5. 拦截UIViewController的popViewController事件

    实现拦截UIViewController的pop操作有两种方式: 自定义实现返回按钮,即设置UIBarButtonItem来实现自定义的返回操作. 创建UINavigatonController的Ca ...

  6. iOS: 在UIViewController 中添加Static UITableView

    如果你直接在 UIViewController 中加入一个 UITableView 并将其 Content 属性设置为 Static Cells,此时 Xcode 会报错: Static table ...

  7. 8. UIViewController

    1. UIViewController 的认识 UIViewController在iOS开发中占据很重要的位置,iOS的整个UI开发的核心思想也是MVC的架构,从UIViewController的命名 ...

  8. 从一个控制器调到另一个控制器的[UIViewController _loadViewFromNibNamed:bundle:]崩溃

    一,现象和分析: 1.崩溃的主要地方是[UIViewController _loadViewFromNibNamed:bundle:] ,是从 A 控制器 push 到 B 控制器后, B 控制器的v ...

  9. UIViewController相关知识

    title: UIViewController 相关知识date: 2015-12-13 11:50categories: IOS tags: UIViewController 小小程序猿我的博客:h ...

随机推荐

  1. Spark on yarn的两种模式 yarn-cluster 和 yarn-client

    从深层次的含义讲,yarn-cluster和yarn-client模式的区别其实就是Application Master进程的区别,yarn-cluster模式下,driver运行在AM(Applic ...

  2. Day1作业---登录接口及多级菜单

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Ma Qing data = { "山东" :{ "济南&qu ...

  3. java继承。顾不了

    总结:为什么结果显示所有数据都重复输出了呢? package com.sa; //java里的几个难以理解的概念.字节码文件.class文件.源文件 //.class文件指的是.编译后产生的字节码文件 ...

  4. Mysql 5.6 MHA (gtid) on Kylin

    mha on Kylinip hostname repl role mha role192.168.19.69 mysql1 master node192.168.19.73 mysql2 slave ...

  5. 【转】link_to中delete无效的问题解决

    问题一 1.问题描述:点击[删除]链接之后,跳到了show页面,完全没有弹出框提示 <%= link_to "删除", product_path(product.id), : ...

  6. 利用Synergy在局域网内让Ubuntu和Windows 7两台机器共用一套键鼠。

    一个主机可以连接多个显示器, 方便自己使用, 但是这只是一个系统分屏显示, 如果想用两台不同系统的电脑, 并且还不想老是在两套键鼠之间来回转换, 那么建议你可以用Synergy软件来实现多台电脑之间的 ...

  7. 空中楼阁 ( House )最短路

    题目描述: 话说Z4阴差阳错地来到了神秘岛.不久,他们发现,这是一个由n个小岛和一个中心岛组成的群岛,群岛之间有m座桥.令他们感到惊讶的是,这些桥并不是固定不变的,经较长时间的观察,发现它们会随时间作 ...

  8. Changing Controller Numbers in Solaris

    If you need to change the controller numbers (c#) that a disk has assigned to it, whether it is for ...

  9. C# Remoting 简单实现

    此处下载源代码(VS2010编译通过)   http://files.cnblogs.com/files/qqhfeng/%E8%BF%9C%E7%A8%8B%E8%B0%83%E7%94%A8%E6 ...

  10. LAMP 2.1Apache不记录指定文件类型日志

    访问日志只需要记地址,不用记录图片. 对无用的图片日志做标记,针对标记做限制.打开 vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 把 Erro ...