日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS实现自定义的弹出视图(popView)

發布時間:2025/3/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS实现自定义的弹出视图(popView) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前段時間,在項目中有個需求是支付完成后,彈出紅包,實現這么一個發紅包的功能。做了最后,實現的效果大致如下:

?

一、使用方法

?


?

整個ViewController的代碼大致如下

?

//

//??SecondViewController.m

//??HWPopTool

//

//??Created by HenryCheng on 16/1/11.

//??Copyright ? 2016年 www.igancao.com. All rights reserved.

//

?

#import "SecondViewController.h"

#import "HWPopTool.h"

?

@interface SecondViewController ()

?

@property (strong, nonatomic) UIView *contentView;

?

@end

?

@implementation SecondViewController

?

- (void)viewDidLoad {

????[super viewDidLoad];

????self.view.backgroundColor = [UIColor whiteColor];

?

????_contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];

????_contentView.backgroundColor = [UIColor clearColor];

?

????UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

????btn.frame = CGRectMake(100, 200, 100, 50);

????btn.backgroundColor = [UIColor greenColor];

????[btn addTarget:self action:@selector(popViewShow) forControlEvents:UIControlEventTouchUpInside];

????[self.view addSubview:btn];

}

?

- (void)popViewShow {

????UIImageView *imageV = [[UIImageView alloc]initWithFrame:_contentView.bounds];

????imageV.image = [UIImage imageNamed:@"jei"];

????[_contentView addSubview:imageV];

?

????[HWPopTool sharedInstance].shadeBackgroundType = ShadeBackgroundTypeSolid;

????[HWPopTool sharedInstance].closeButtonType = ButtonPositionTypeRight;

????[[HWPopTool sharedInstance] showWithPresentView:_contentView animated:YES];

?

}

?

- (void)didReceiveMemoryWarning {

????[super didReceiveMemoryWarning];

????// Dispose of any resources that can be recreated.

}

?

@end

?

我們引入了HWPopTool.h,并且創建了一個button,點擊button的方法是popViewShow,我們來看一下這里面的代碼:

?

- (void)popViewShow {

????UIImageView *imageV = [[UIImageView alloc]initWithFrame:_contentView.bounds];

????imageV.image = [UIImage imageNamed:@"jei"];

????[_contentView addSubview:imageV];

?

????[HWPopTool sharedInstance].shadeBackgroundType = ShadeBackgroundTypeSolid;

????[HWPopTool sharedInstance].closeButtonType = ButtonPositionTypeRight;

????[[HWPopTool sharedInstance] showWithPresentView:_contentView animated:YES];

?

}

?

這里在_contentView上放了一個imageView,然后我們設置了shadeBackgroundType和closeButtonType以后,下面一句代碼就是展示出來popView。

這里主要就是我們彈出一個view,至于這個view多大,上面放什么,都是由你自己決定的。

?

二、關于HWPopTool里面的一些屬性和方法

?


?

先來看一下HWPopTool.h

?

?

//

//??HWPopTool.h

//??HWPopTool

//

//??Created by HenryCheng on 16/1/11.

//??Copyright ? 2016年 www.igancao.com. All rights reserved.

//

?

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

/**

*??關閉按鈕的位置

*/

typedef NS_ENUM(NSInteger, ButtonPositionType) {

????/**

???? *??無

???? */

????ButtonPositionTypeNone = 0,

????/**

???? *??左上角

???? */

????ButtonPositionTypeLeft = 1 << 0,

????/**

???? *??右上角

???? */

????ButtonPositionTypeRight = 2 << 0

};

/**

*??蒙板的背景色

*/

typedef NS_ENUM(NSInteger, ShadeBackgroundType) {

????/**

???? *??漸變色

???? */

????ShadeBackgroundTypeGradient = 0,

????/**

???? *??固定色

???? */

????ShadeBackgroundTypeSolid = 1 << 0

};

?

typedef void(^completeBlock)(void);

?

@interface HWPopTool : NSObject

?

@property (strong, nonatomic) UIColor *popBackgroudColor;//彈出視圖的背景色

@property (assign, nonatomic) BOOL tapOutsideToDismiss;//點擊蒙板是否彈出視圖消失

@property (assign, nonatomic) ButtonPositionType closeButtonType;//關閉按鈕的類型

@property (assign, nonatomic) ShadeBackgroundType shadeBackgroundType;//蒙板的背景色

?

/**

*??創建一個實例

*

*??@return CHWPopTool

*/

+ (HWPopTool *)sharedInstance;

/**

*??彈出要展示的View

*

*??@param presentView show View

*??@param animated????是否動畫

*/

- (void)showWithPresentView:(UIView *)presentView animated:(BOOL)animated;

/**

*??關閉彈出視圖

*

*??@param complete complete block

*/

- (void)closeWithBlcok:(void(^)())complete;

?

@end

?

由于之前寫的比較倉促,今天趁著空余時間又把代碼整理了一遍,比如關閉之后的回調,之前用delegate實現的,今天又用block重新寫的,簡潔一點吧,另外基本上所有的方法、屬性、枚舉我都有注釋,算是個個人習慣吧。

這里面有幾點需要說明的是:

?

  • 1.ShadeBackgroundType是蒙板的背景色屬性,有固定的和漸變的(ShadeBackgroundTypeGradient),關于這個漸變,有興趣的可以研究一下CAGradientLayer,還是很有趣的,在后來的文章中也會說到。

  • 2.tapOutsideToDismiss這個是設置點擊蒙板,popView消失不消失的屬性,默認的是YES

  • 3.- (void)closeWithBlcok:(void(^)())complete這個方法,是關閉后的回調,比如說發送紅包以后,等popView消失以后回到上一頁的這種。?

三、最后

https://github.com/Loveway/HWPopTool

?

轉載于:https://www.cnblogs.com/fengmin/p/5887438.html

總結

以上是生活随笔為你收集整理的iOS实现自定义的弹出视图(popView)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。