天天看點

iOS學習筆記-090.彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取

  • 彩票05購彩大廳2_自定義蒙版popMenuUIView分類抽取
    • 一圖示
    • 二自定義蒙版
      • 1 分析
      • 2 QWMCoverh
      • 3 QWMCoverm
    • 三自定義popMenu
      • 1 分析
      • 2 QWMPopMenuh
      • 2 QWMPopMenum
    • 四QWMHallTableViewControllerm 類修改
    • 五UIView分類抽取
      • 1 UIViewFrameh
      • 2 UIViewFramem
    • 六pch編輯
      • 1 杞文明彩票pch
      • 2 在Xcode5以後幹掉了如想用需要手動配置

彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取

一、圖示

iOS學習筆記-090.彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取

二、自定義蒙版

2.1 分析

  1. 蒙版一般添加在keyWidown上面
  2. pop菜單加載keyWidown上面,如果添加到蒙版上面,會随着父控件的透明而透明
  3. 解決不透明的方法,不要修改alpha,修改背景顔色就可以了

我們的蒙版需要顯示和隐藏兩個方法,顯示的時候添加到keyWindow上面,隐藏的時候從keyWindow上面移除。

2.2 QWMCover.h

//
//  QWMCover.h
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/23.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface QWMCover : UIView
//顯示
+(void)show;
//隐藏
+(void)hide;
@end
           

2.3 QWMCover.m

//
//  QWMCover.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/23.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "QWMCover.h"

#define QWMKeyWindow [UIApplication sharedApplication].keyWindow

@implementation QWMCover

+(void)show{
    //1.建立蒙版
    QWMCover *cover = [[QWMCover alloc]init];

    //2.把蒙版添加到視窗上
    [QWMKeyWindow addSubview:cover];

    //3.設定尺寸
    cover.frame = [UIScreen mainScreen].bounds;
    //設定背景
    cover.backgroundColor = [UIColor colorWithRed: green: blue: alpha:];
}

+(void)hide{
    //隐藏蒙版
    //從window中删除它
    for (UIView *view in QWMKeyWindow.subviews) {
        if ([view isKindOfClass:[QWMCover class]]) {
            [view removeFromSuperview];
            break;
        }
    }
}

@end
           

三、自定義popMenu

3.1 分析

pop菜單加載keyWidown上面,如果添加到蒙版上面,會随着父控件的透明而透明。除此之外如果添加到蒙版上面依賴會很強,是以我們添加到keyWindow上面

popMenu是靜态,是以可以使用xib描述

iOS學習筆記-090.彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取
//從xib建立popMenu
QWMPopMenu *popMenu = [[NSBundle mainBundle]loadNibNamed:@"QWMPopMenu" owner:nil options:nil][0];
           

popMenu需要有顯示的方法,這個方法就是建立一個popMenu添加到keyWindow中。

popMenu需要有隐藏的方法,這個方法就是從keyWindow中移除popMenu,并且有消失的動畫,動畫結束以後,可以傳入一個代碼塊。

popMenu需要有點選關閉的方法,這個方法中我們把點選傳給代理

3.2 QWMPopMenu.h

//
//  QWMPopMenu.h
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/23.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import <UIKit/UIKit.h>
@class QWMPopMenu;

@protocol QWMPopMenuDelegate <NSObject>

-(void)popMenuDidCloseBtn:(QWMPopMenu*)popMenu;
@end

@interface QWMPopMenu : UIView
@property(nonatomic,weak) id<QWMPopMenuDelegate> delegate;

+(instancetype)showInCenter:(CGPoint)center;

-(void)hideInCenter:(CGPoint)center complete:(void(^)())complete;
@end
           

3.2 QWMPopMenu.m

//
//  QWMPopMenu.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/23.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "QWMPopMenu.h"

@implementation QWMPopMenu

+(instancetype)showInCenter:(CGPoint)center{
    //從xib建立popMenu
    QWMPopMenu *popMenu = [[NSBundle mainBundle]loadNibNamed:@"QWMPopMenu" owner:nil options:nil][];
    popMenu.center = center;
    //添加到視窗中
    [[UIApplication sharedApplication].keyWindow addSubview:popMenu];
    return popMenu;
}

-(void)hideInCenter:(CGPoint)center complete:(void (^)())complete{
    [UIView animateWithDuration: animations:^{
        self.center = CGPointMake(, );
        self.transform = CGAffineTransformMakeScale(, );
    } completion:^(BOOL finished) {
        //1、移除自己
        [self removeFromSuperview];
        //2。移除蒙版
        // block當做參數傳遞, 如果值為空,直接調用崩潰
        if(complete){
            complete();
        }
    }];
}

- (IBAction)close:(id)sender {
    //通知外界點選了關閉
    if ([self.delegate respondsToSelector:@selector(popMenuDidCloseBtn:)]) {
        [self.delegate popMenuDidCloseBtn:self];
    }
}

@end
           

四、QWMHallTableViewController.m 類修改

這個類裡面,我們主要幹兩個事情,點選導覽列左邊顯示蒙版和popMenu菜單。點選popMenu關閉按鈕,隐藏popMenu和蒙版

//
//  QWMHallTableViewController.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/7/24.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "QWMHallTableViewController.h"
#import "UIImage+QWM.h"
#import "QWMCover.h"
#import "QWMPopMenu.h"

@interface QWMHallTableViewController () <QWMPopMenuDelegate>

@end

@implementation QWMHallTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 在iOS7 之後導覽列上圖檔,預設被渲染
    //1.添加導覽列
//    UIImage *image = [UIImage imageNamed:@"CS50_activity_image"];
//    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageWithRenderOriginalName:@"CS50_activity_image"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonClick)];
}

-(void)leftBarButtonClick{
    NSLog(@"%s line = %d",__FUNCTION__,__LINE__);
    //顯示蒙版
    [QWMCover show];

    //添加popMenu
    QWMPopMenu *popMenu = [QWMPopMenu showInCenter:self.view.center];
    popMenu.delegate = self;
}

-(void)popMenuDidCloseBtn:(QWMPopMenu *)popMenu{
    //隐藏蒙版
    [popMenu hideInCenter:CGPointMake(, ) complete:^{
        [QWMCover hide];
    }];
}

@end
           

五、UIView分類抽取

這個分類我們要是就是添加 x,y,width,height 的擷取和設定

5.1 UIView+Frame.h

//
//  UIView+Frame.h
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/23.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIView (Frame)

// 在分類中 @property 隻會生成get, set方法,并不會生成下劃線的成員屬性

@property(nonatomic,assign) CGFloat width;

@property(nonatomic,assign) CGFloat height;

@property(nonatomic,assign) CGFloat x;

@property(nonatomic,assign) CGFloat y;

@end
           

5.2 UIView+Frame.m

//
//  UIView+Frame.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/23.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "UIView+Frame.h"

@implementation UIView (Frame)

-(CGFloat)width{
    return self.frame.size.width;
}

-(void)setWidth:(CGFloat)width{
    CGRect rect = self.frame;
    rect.size.width= width;
    self.frame = rect;
}

-(CGFloat)height{
    return self.frame.size.height;
}

-(void)setHeight:(CGFloat)height{
    CGRect rect = self.frame;
    rect.size.height= height;
    self.frame = rect;
}

-(CGFloat)x{
    return self.frame.origin.x;
}

-(void)setX:(CGFloat)x{
    CGRect rect = self.frame;
    rect.origin.x = x;
    self.frame = rect;
}

-(CGFloat)y{
    return self.frame.origin.y;
}

-(void)setY:(CGFloat)y{
    CGRect rect = self.frame;
    rect.origin.y = y;
    self.frame = rect;
}

@end
           

六、pch編輯

建立一個 杞文明彩票.pch

6.1 杞文明彩票.pch

//
//  杞文明彩票.pch
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/24.
//  Copyright © 2017年 杞文明. All rights reserved.
//
// poch 為什麼不能用
// 在Xcode5以後幹掉了,如想用,需要手動配置
#ifndef ______pch
#define ______pch

#ifdef __OBJC__ // 隻有oc檔案才導入
#import "UIView+Frame.h"
#import "UIImage+QWM.h"


#endif

#endif /* ______pch */

           

6.2 在Xcode5以後幹掉了,如想用,需要手動配置

iOS學習筆記-090.彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取彩票05——購彩大廳2_自定義蒙版、popMenu、UIView分類抽取

如圖,按照 1-6的步驟操作,5中修改為true,6為.pch的位置