Implementation of View_ios with arrows based on IOS

Source: Internet
Author: User

I use DrawRect to stretch the view (which is described here??), and the effect chart also implements a micro-letter view effect that you can take a look at.

Create a view that inherits from UIView. h file

Backgoundview
@property (nonatomic, Strong) UIView * _nonnull Backgoundview;
Titles
@property (nonatomic, Strong) Nsarray * _nonnull DataArray;
Images
@property (nonatomic, Strong) Nsarray * _nonnull images;
Height
@property (nonatomic, assign) cgfloat row_height;
Font
@property (nonatomic, assign) cgfloat fontsize;
TextColor
@property (nonatomic, assign) Uicolor * _nonnull Titletextcolor;
Delegate
@property (nonatomic, assign) ID <selectIndexPathDelegate> _nonnull delegate;
Initialization method
-(Instancetype _nonnull) Initwithorigin: (Cgpoint) Origin
width: (cgfloat) Width
Height: ( cgfloat) Height
type: (xtdirectiontype) Type
Color: ( Uicolor * _nonnull) color;
-(void) Popview;
-(void) dismiss;

##.m Implementation Section

Define the macros to use

#define ScreenWidth [UIScreen mainscreen].bounds.size.width
#define ScreenHeight [UIScreen mainscreen]. Bounds.size.height
#define LENGTH 5
#define LENGTH2 15
@property (nonatomic, assign) Cgpoint origin;     Arrow position
@property (nonatomic, assign) cgfloat height;     The height of the view
@property (nonatomic, assign) cgfloat width;      The width of the view
@property (nonatomic, assign) xtdirectiontype type;    Arrow Position Type
@property (nonatomic, strong) UITableView *tableview;   TableView of padding

Custom Initialization method

-(Instancetype) Initwithorigin: (Cgpoint) Origin width: (cgfloat) Width height: (cgfloat) Height Type: (xtdirectiontype) Type color: (Uicolor *) color
{

self = [Super Initwithframe:cgrectmake (0, 0, ScreenWidth, screenheight)];
if (self) {
Self.backgroundcolor = [Uicolor clearcolor];
Self.origin = origin;
Self.width = width;
self.height = height;
Self.type = type;
Self.backgoundview = [[UIView alloc] Initwithframe:cgrectmake (origin.x, origin.y, width, height)];
Self.backGoundView.backgroundColor = color;
[Self addSubview:self.backGoundView];
[Self.backgoundview AddSubview:self.tableView];
}
return self;
}

DrawRect

#pragma mark-drawrect-(void) DrawRect: (cgrect) Rect {//Drawing code CGCONTEXTREF context = Uigraphicsgetcurrentconte

XT ();
Switch (self.type) {case Xttypeofupleft:case xttypeofupcenter:case xttypeofupright:{{cgfloat startx = self.origin.x;
CGFloat starty = SELF.ORIGIN.Y;
Cgcontextmovetopoint (context, startx, starty);
Cgcontextaddlinetopoint (context, StartX + length, starty + length);
Cgcontextaddlinetopoint (context, startx-length, Starty + Length);
} break;
Case Xttypeofdownleft:case Xttypeofdowncenter:case xttypeofdownright: {{cgfloat startx = self.origin.x;
CGFloat starty = SELF.ORIGIN.Y;
Cgcontextmovetopoint (context, startx, starty);
Cgcontextaddlinetopoint (context, startx-length, starty-length);
Cgcontextaddlinetopoint (context, StartX + Length, starty-length);
} break;
Case Xttypeofleftup:case Xttypeofleftcenter:case Xttypeofleftdown: {{cgfloat startx = self.origin.x;
CGFloat starty = SELF.ORIGIN.Y;
Cgcontextmovetopoint (context, startx, starty); CgconTextaddlinetopoint (context, StartX + Length, starty-length);
Cgcontextaddlinetopoint (context, StartX + length, starty + length);
} break;
Case Xttypeofrightup:case Xttypeofrightcenter:case Xttypeofrightdown: {{cgfloat startx = self.origin.x;
CGFloat starty = SELF.ORIGIN.Y;
Cgcontextmovetopoint (context, startx, starty);
Cgcontextaddlinetopoint (context, startx-length, starty-length);
Cgcontextaddlinetopoint (context, startx-length, Starty + Length);
} break;
} cgcontextclosepath (context);
[Self.backGoundView.backgroundColor Setfill];
[Self.backgroundcolor Setstroke];
Cgcontextdrawpath (context, kcgpathfillstroke); }

Pop-up View

#pragma mark-popview-(void) Popview {//Sync display child controls (views) and (self) nsarray *results = [Self.backgoundview subviews]; (UIView *view in results)
{[View sethidden:yes];}
UIWindow *windowview = [UIApplication Sharedapplication].keywindow;
[Windowview addsubview:self];  Switch (self.type) {case Xttypeofupleft: {{self.backGoundView.frame = CGRectMake (self.origin.x, SELF.ORIGIN.Y + Length,
0, 0);
CGFloat origin_x = self.origin.x-length2;
CGFloat origin_y = self.origin.y + Length;
CGFloat size_width = self.width;
CGFloat size_height = self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break;
Case Xttypeofupcenter: {{self.backGoundView.frame = CGRectMake (self.origin.x, SELF.ORIGIN.Y + Length, 0, 0);
CGFloat origin_x = SELF.ORIGIN.X-SELF.WIDTH/2;
CGFloat origin_y = self.origin.y + Length;
CGFloat size_width = self.width;
CGFloat size_height = self.height; [Self startanimateview_x:origin_x _y:origin_y origin_width:siZe_width Origin_height:size_height];
} break;
Case Xttypeofupright: {{self.backGoundView.frame = CGRectMake (self.origin.x, SELF.ORIGIN.Y + Length, 0, 0);
CGFloat origin_x = self.origin.x + Length2;
CGFloat origin_y = self.origin.y + Length;
CGFloat size_width =-self.width;
CGFloat size_height = self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break;
Case Xttypeofdownleft: {{self.backGoundView.frame = CGRectMake (self.origin.x, self.origin.y-length, 0, 0);
CGFloat origin_x = self.origin.x-length2;
CGFloat origin_y = self.origin.y-length;
CGFloat size_width = self.width;
CGFloat size_height =-self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break;
Case Xttypeofdowncenter: {{self.backGoundView.frame = CGRectMake (self.origin.x, self.origin.y-length, 0, 0);
CGFloat origin_x = SELF.ORIGIN.X-SELF.WIDTH/2; CGFloat origin_y = Self.origin.y-leNgth;
CGFloat size_width = self.width;
CGFloat size_height =-self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break;
Case Xttypeofdownright: {{self.backGoundView.frame = CGRectMake (self.origin.x, self.origin.y-length, 0, 0);
CGFloat origin_x = self.origin.x-self.width + Length2;
CGFloat origin_y = self.origin.y-length;
CGFloat size_width = self.width;
CGFloat size_height =-self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break;
Case Xttypeofleftup: {{self.backGoundView.frame = CGRectMake (self.origin.x + Length, self.origin.y, 0, 0);
CGFloat origin_x = self.origin.x + Length;
CGFloat origin_y = self.origin.y-length2;
CGFloat size_width = self.width;
CGFloat size_height = self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break; Case Xttypeofleftcenter: {{self.backGoundView.frame= CGRectMake (self.origin.x + Length, self.origin.y, 0, 0);
CGFloat origin_x = self.origin.x + Length;
CGFloat origin_y = SELF.ORIGIN.Y-SELF.HEIGHT/2;
CGFloat size_width = self.width;
CGFloat size_height = self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break;
Case Xttypeofleftdown: {{self.backGoundView.frame = CGRectMake (self.origin.x + Length, self.origin.y, 0, 0);
CGFloat origin_x = self.origin.x + Length;
CGFloat origin_y = self.origin.y-self.height + Length2;
CGFloat size_width = self.width;
CGFloat size_height = self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break;
Case Xttypeofrightup: {{self.backGoundView.frame = CGRectMake (self.origin.x-length, SELF.ORIGIN.Y, 0, 0);
CGFloat origin_x = self.origin.x-length;
CGFloat origin_y = self.origin.y-length2;
CGFloat size_width =-self.width;
CGFloat size_height = self.height; [Self Startanimateview_x:origin_x _y:origin_y origin_width:size_width Origin_height:size_height];
} break;
Case Xttypeofrightcenter: {{self.backGoundView.frame = CGRectMake (self.origin.x-length, SELF.ORIGIN.Y, 0, 0);
CGFloat origin_x = self.origin.x-length;
CGFloat origin_y = SELF.ORIGIN.Y-SELF.HEIGHT/2;
CGFloat size_width =-self.width;
CGFloat size_height = self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break;
Case Xttypeofrightdown: {{self.backGoundView.frame = CGRectMake (self.origin.x-length, SELF.ORIGIN.Y, 0, 0);
CGFloat origin_x = self.origin.x-length;
CGFloat origin_y = self.origin.y-self.height + Length2;
CGFloat size_width =-self.width;
CGFloat size_height = self.height;
[Self startanimateview_x:origin_x _y:origin_y origin_width:size_width origin_height:size_height];
} break; }
}
}

#pragma mark-

-(void) startanimateview_x: (cgfloat) x
_y: (cgfloat) y
origin_width: (cgfloat) Width
origin_height: ( CGFloat) Height
{
[uiview animatewithduration:0.25 animations:^{
self.backGoundView.frame = CGRectMake ( x, y, width, height);
completion:^ (BOOL finished) {
Nsarray *results = [Self.backgoundview subviews];
For (UIView *view in results) {
[view Sethidden:no];}}
];
}

Click on the empty space to recycle

#pragma mark-
(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event
{
if ( ! [[Touches Anyobject].view IsEqual:self.backGoundView]) {
[self dismiss];
} 
}
#pragma mark-
-(void) dismiss
{
/**
* Delete child controls on Backgroundview/
nsarray *results = [ Self.backgoundview Subviews];
For (UIView *view in results) {
[view Removefromsuperview]; 
}
[UIView animatewithduration:0.25 animations:^{
//
Self.backGoundView.frame = CGRectMake (self.origin.x, SELF.ORIGIN.Y, 0, 0);
completion:^ (BOOL finished) {
//
[self removefromsuperview];
}];

The internal TableView

#pragma mark-(UITableView *) TableView {if (!_tableview) {_tableview = [[UITableView alloc] Initwithframe:cgrectmake
(0, 0, self.backgoundview.frame.size.width-5, self.backGoundView.frame.size.height) Style:uitableviewstyleplain];
_tableview.datasource = self;
_tableview.backgroundcolor = [Uicolor Clearcolor];
_tableview.delegate = self;
return _tableview; #pragma mark-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return
Self.dataArray.count; #pragma mark-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {if (sel F.row_height = = 0) {return 44;}
else{return Self.row_height}}

#pragma mark-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {
static NSString *cellidentifier = @ "CellIdentifier2";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (!cell) {cell = [[UITableViewCell alloc] INitwithstyle:uitableviewcellstylesubtitle Reuseidentifier:cellidentifier];
} Cell.backgroundcolor = [Uicolor Clearcolor];
Cell.imageView.image = [UIImage Imagenamed:self.images[indexpath.row]];
Cell.textLabel.text = Self.dataarray[indexpath.row];
Cell.textLabel.font = [Uifont systemFontOfSize:self.fontSize];
Cell.textLabel.textColor = Self.titletextcolor;
return cell;
//Want to implement the click to do other operations, here is the Protocol-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {if (self.delegate && [self.delegate respondstoselector: @selector (selectindexpathrow:)]) {[self.delegate
SelectIndexPathRow:indexPath.row]; }
}

# #在. h file also declares an agreement

@protocol selectindexpathdelegate <NSObject>
-(void) Selectindexpathrow: (nsinteger) index;
@end

Use

@interface Viewcontroller () <selectIndexPathDelegate>

# #你可以在btn的点击方法里这样写

Supports multiple types
/**
xttypeofupleft,//  upper left
xttypeofupcenter,//Ober Chong
xttypeofupright,//up-right

Xttypeofdownleft,//down left xttypeofdowncenter,//down xttypeofdownright,//down
right

xttypeofleftup,  //top Left
Xttypeofleftcenter,//Zozhong
Xttypeofleftdown,///lower left

xttypeofrightup,//Right upper
xttypeofrightcenter,//right Middle Xttypeofrightdown,//
lower right
*/

Cgpoint point = Cgpointmake (_custombtn.center.x,_custombtn.frame.origin.y +);
Xtpopview *view1 = [[Xtpopview alloc] Initwithorigin:point width:130 height:40 * 4 type:xttypeofupright Color:[UIColor col orwithred:0.2737 green:0.2737 blue:0.2737 alpha:1.0]];
View1.dataarray = @[@ "Initiate group chat", @ "add friend", @ "sweep", @ "collect payment"];
View1.images = @[@ "Initiate group chat", @ "add friend", @ "sweep", @ "payment"];
View1.fontsize =;
View1.row_height = n;
View1.titletextcolor = [Uicolor whitecolor];
View1.delegate = self;
[View1 Popview];

# #想要使用点击方法 as long as it's a way to implement the Protocol.

-(void) Selectindexpathrow: (nsinteger) index
{
switch (index) {case
0:
{
NSLog (@ "click 0 ...") );
}
break;
Case 1:
{
NSLog (@ "CLIKC 1 ...");
}
break;
Case 2:
{
NSLog (@ "CLIKC 2 ...");
}
break;
Case 3:
{
NSLog (@ "CLIKC 3 ...");
}
break;
Default: Break
;
}
}

Summarize

The above is based on iOS to implement the full view of the arrow, I hope to develop iOS can help.

Related Article

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.