iOS implementing a custom pop-up view (Popview)

Source: Internet
Author: User

Some time ago, in the project there is a need to pay the completion, pop red envelopes, to achieve such a red envelope function. Finally, the effect of the implementation is roughly as follows:

I. Methods of Use

The entire Viewcontroller code is roughly as follows

//

Secondviewcontroller.m

Hwpoptool

//

Created by Henrycheng on 16/1/11.

COPYRIGHT©2016 year 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, (+ )];

_contentview. BackgroundColor = [uicolor clearcolor];

UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];

btn. frame = CGRectMake (+, +, + );

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, can be recreated.

}

@End

We introduced HWPopTool.h and created a button, and the way to click on the button is Popviewshow, let's take a look at the code inside:

- (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];

}

Here on the _contentview put a imageview, and then we set the Shadebackgroundtype and Closebuttontype, the following code is displayed Popview.

The main thing here is that we pop up a view, as to how big the view is, what it puts on it, it's up to you to decide.

Ii. about some properties and methods in Hwpoptool

Let's take a look at HWPopTool.h

//

HWPopTool.h

Hwpoptool

//

Created by Henrycheng on 16/1/11.

COPYRIGHT©2016 year www.igancao.com. All rights reserved.

//

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

/**

* The position of the close button

*/

typedef Ns_enum(nsinteger, buttonpositiontype) {

/**

* No

*/

buttonpositiontypenone = 0,

/**

* Upper left corner

*/

buttonpositiontypeleft = 1 << 0,

/**

* Upper right corner

*/

buttonpositiontyperight = 2 << 0

};

/**

* Background color of mask

*/

typedef Ns_enum(nsinteger, shadebackgroundtype) {

/**

* Fade Color

*/

shadebackgroundtypegradient = 0,

/**

* Fixed Color

*/

shadebackgroundtypesolid = 1 << 0

};

typedef void(^completeblock) (void);

@interface hwpoptool : NSObject

@property (strong, nonatomic) uicolor *popbackgroudcolor ; //pop-up view background color

@property (assign, nonatomic) BOOL Tapoutsidetodismiss ; //Click Mask to see if pop-up view disappears

@property (assign, nonatomic) buttonpositiontype closebuttontype ; //Close button type

@property (assign, nonatomic) shadebackgroundtype Shadebackgroundtype; Background color of the mask

/**

* Create an instance

*

* @return Chwpoptool

*/

+ (hwpoptool *)sharedinstance;

/**

* Pop up the view to show

*

* @param presentview Show View

* @param animated is animated

*/

- (void)showwithpresentview:(UIView *)Presentview Animated :(BOOL)animated;

/**

* Close pop-up view

*

* @param complete block

*/

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

@end

As a result of the relatively hasty writing, today while free time and put the code again, such as the callback after the closure, before the implementation of delegate, and today with block re-write, concise point, in addition to basically all the methods, attributes, enumerations I have comments, is a personal habit.

Here are a few things to note:

    • 1.ShadeBackgroundType is the background color property of the mask, there are fixed and gradient (shadebackgroundtypegradient), about this gradient, interested can study the Cagradientlayer, or very interesting, It will also be said in later articles.

    • 2.tapOutsideToDismiss This is set to click the Mask, Popview disappears not disappear property, the default is Yes

    • 3.-(void) Closewithblcok: (void (^) ()) Complete this method is a closed callback, such as after sending a red envelope, such as after the Popview disappears to the previous page.

Third, the last

Https://github.com/Loveway/HWPopTool

iOS implementing a custom pop-up view (Popview)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.