Skip to content

Commit

Permalink
-增加箭头居中位置
Browse files Browse the repository at this point in the history
  • Loading branch information
wuchuanyu committed Jun 8, 2016
1 parent 715bbe9 commit 689e90b
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@
## 带箭头的矩形View
可自定义属性(所有属性均可通过xib调整):圆角、画线宽度、虚线线宽、虚线间隔、边框颜色、填充颜色、箭头宽高、箭头位置、箭头偏移

箭头位置说明:

enum | 说明
------------ | -------------
TopLeft | 上边居左,arrowOffset为距左边偏移量
TopMiddle | 上边居中,arrowOffset无效
TopRight | 上边居右,arrowOffset为距右边偏移量
BottomLeft | 下边居左,arrowOffset为距左边偏移量
BottomMiddle | 下边居中,arrowOffset无效
BottomRight | 下边居右,arrowOffset为距右边偏移量
LeftTop | 左边居上,arrowOffset为距上边偏移量
LeftMiddle | 左边居中,arrowOffset无效
LeftBottom | 左边居右,arrowOffset为距下边偏移量
RightTop | 右边居上,arrowOffset为距上边偏移量
RightMiddle | 右边居中,arrowOffset无效
RightBottom | 右边居下,arrowOffset为距下边偏移量

![WCYArrowRectView](http://upload-images.jianshu.io/upload_images/1449203-0aad824f5f43edba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/500)
Binary file not shown.
12 changes: 12 additions & 0 deletions WCYArrowRectView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ class ViewController: UIViewController {
arrowRectView7.strokeColor = UIColor(red: 55/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1.0)
arrowRectView7.fillColor = UIColor(red: 55/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1.0)
self.view.addSubview(arrowRectView7)

let arrowRectView8 = WCYArrowRectView(frame: CGRectMake(20, 520, 80, 100))
arrowRectView8.arrowPos = .LeftMiddle
arrowRectView8.arrowWidth = 20
arrowRectView8.arrowHeight = 10
self.view.addSubview(arrowRectView8)

let arrowRectView9 = WCYArrowRectView(frame: CGRectMake(140, 520, 80, 100))
arrowRectView9.arrowPos = .RightMiddle
arrowRectView9.arrowWidth = 20
arrowRectView9.arrowHeight = 10
self.view.addSubview(arrowRectView9)
}

override func didReceiveMemoryWarning() {
Expand Down
122 changes: 121 additions & 1 deletion WCYArrowRectView/WCYArrowRectView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@

import UIKit

enum WCYArrowPosition : Int {
public enum WCYArrowPosition : Int {
case TopLeft
case TopMiddle
case TopRight
case BottomLeft
case BottomMiddle
case BottomRight
case LeftTop
case LeftMiddle
case LeftBottom
case RightTop
case RightMiddle
case RightBottom
}

Expand Down Expand Up @@ -58,18 +62,26 @@ class WCYArrowRectView: UIView {
switch arrowPos {
case .TopLeft:
path = topLeftPath()
case .TopMiddle:
path = topMiddlePath()
case .TopRight:
path = topRightPath()
case .BottomLeft:
path = bottomLeftPath()
case .BottomMiddle:
path = bottomMiddlePath()
case .BottomRight:
path = bottomRightPath()
case .LeftTop:
path = leftTopPath()
case .LeftMiddle:
path = leftMiddlePath()
case .LeftBottom:
path = leftBottomPath()
case .RightTop:
path = rightTopPath()
case .RightMiddle:
path = rightMiddlePath()
case .RightBottom:
path = rightBottomPath()
}
Expand Down Expand Up @@ -117,6 +129,33 @@ class WCYArrowRectView: UIView {
return path
}

private func topMiddlePath() -> CGPath {
let corner: CGFloat = self.corner
let width = CGRectGetWidth(self.frame)
let height = CGRectGetHeight(self.frame)
let minX = strokeWidth/2
let maxX = width - strokeWidth/2
let minY = strokeWidth/2
let maxY = height - strokeWidth/2
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, minX + corner, minY + arrowHeight)
//arrow
CGPathAddLineToPoint(path, nil, (minX+maxX)/2 - arrowWidth/2, minY + arrowHeight)
CGPathAddLineToPoint(path, nil, (minX+maxX)/2, minY)
CGPathAddLineToPoint(path, nil, (minX+maxX)/2 + arrowWidth/2, minY + arrowHeight)
//arrow end
CGPathAddLineToPoint(path, nil, maxX - corner , minY + arrowHeight)
CGPathAddArcToPoint(path, nil, maxX , minY + arrowHeight, maxX, minY + corner + arrowHeight, corner)
CGPathAddLineToPoint(path, nil, maxX , maxY - corner)
CGPathAddArcToPoint(path, nil, maxX, maxY, maxX - corner, maxY, corner)
CGPathAddLineToPoint(path, nil, corner, maxY)
CGPathAddArcToPoint(path, nil, minX, maxY, minX, maxY - corner, corner)
CGPathAddLineToPoint(path, nil, minX, corner + arrowHeight)
CGPathAddArcToPoint(path, nil, minX, minY + arrowHeight, minX + corner, minY + arrowHeight, corner)
CGPathCloseSubpath(path)
return path
}

private func topRightPath() -> CGPath {
let corner: CGFloat = self.corner
let width = CGRectGetWidth(self.frame)
Expand Down Expand Up @@ -171,6 +210,33 @@ class WCYArrowRectView: UIView {
return path
}

private func bottomMiddlePath() -> CGPath {
let corner: CGFloat = self.corner
let width = CGRectGetWidth(self.frame)
let height = CGRectGetHeight(self.frame)
let minX = strokeWidth/2
let maxX = width - strokeWidth/2
let minY = strokeWidth/2
let maxY = height - strokeWidth/2
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, minX + corner, minY)
CGPathAddLineToPoint(path, nil, maxX - corner, minY)
CGPathAddArcToPoint(path, nil, maxX, minY, maxX, corner + minY, corner)
CGPathAddLineToPoint(path, nil, maxX, maxY - corner - arrowHeight)
CGPathAddArcToPoint(path, nil, maxX, maxY - arrowHeight, maxX - corner, maxY - arrowHeight, corner)
//arrow
CGPathAddLineToPoint(path, nil, (minX+maxX)/2 + arrowWidth/2, maxY - arrowHeight)
CGPathAddLineToPoint(path, nil, (minX+maxX)/2, maxY)
CGPathAddLineToPoint(path, nil, (minX+maxX)/2 - arrowWidth/2, maxY - arrowHeight)
//arrow end
CGPathAddLineToPoint(path, nil, corner, maxY - arrowHeight)
CGPathAddArcToPoint(path, nil, minX, maxY - arrowHeight, minX, maxY - corner - arrowHeight, corner)
CGPathAddLineToPoint(path, nil, minX, corner)
CGPathAddArcToPoint(path, nil, minX, minY, minX + corner, minY, corner)
CGPathCloseSubpath(path)
return path
}

private func bottomRightPath() -> CGPath {
let corner: CGFloat = self.corner
let width = CGRectGetWidth(self.frame)
Expand Down Expand Up @@ -225,6 +291,33 @@ class WCYArrowRectView: UIView {
return path
}

private func leftMiddlePath() -> CGPath {
let corner: CGFloat = self.corner
let width = CGRectGetWidth(self.frame)
let height = CGRectGetHeight(self.frame)
let minX = strokeWidth/2
let maxX = width - strokeWidth/2
let minY = strokeWidth/2
let maxY = height - strokeWidth/2
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, minX + corner + arrowHeight, minY)
CGPathAddLineToPoint(path, nil, maxX - corner, minY)
CGPathAddArcToPoint(path, nil, maxX, minY, maxX, corner + minY, corner)
CGPathAddLineToPoint(path, nil, maxX, maxY - corner)
CGPathAddArcToPoint(path, nil, maxX, maxY, maxX - corner, maxY, corner)
CGPathAddLineToPoint(path, nil, corner + arrowHeight, maxY)
CGPathAddArcToPoint(path, nil, arrowHeight, maxY, arrowHeight, maxY - corner, corner)
//arrow
CGPathAddLineToPoint(path, nil, arrowHeight, (minY+maxY)/2 + arrowWidth/2)
CGPathAddLineToPoint(path, nil, minX, (minY+maxY)/2)
CGPathAddLineToPoint(path, nil, arrowHeight, (minY+maxY)/2 - arrowWidth/2)
//arrow end
CGPathAddLineToPoint(path, nil, arrowHeight, corner)
CGPathAddArcToPoint(path, nil, arrowHeight, minY, corner + arrowHeight, minY, corner)
CGPathCloseSubpath(path)
return path
}

private func leftBottomPath() -> CGPath {
let corner: CGFloat = self.corner
let width = CGRectGetWidth(self.frame)
Expand Down Expand Up @@ -279,6 +372,33 @@ class WCYArrowRectView: UIView {
return path
}

private func rightMiddlePath() -> CGPath {
let corner: CGFloat = self.corner
let width = CGRectGetWidth(self.frame)
let height = CGRectGetHeight(self.frame)
let minX = strokeWidth/2
let maxX = width - strokeWidth/2
let minY = strokeWidth/2
let maxY = height - strokeWidth/2
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, minX + corner, minY)
CGPathAddLineToPoint(path, nil, maxX - corner - arrowHeight, minY)
CGPathAddArcToPoint(path, nil, maxX - arrowHeight, minY, maxX - arrowHeight, corner + minY, corner)
//arrow
CGPathAddLineToPoint(path, nil, maxX - arrowHeight, (minY+maxY)/2 - arrowWidth/2)
CGPathAddLineToPoint(path, nil, maxX, (minY+maxY)/2)
CGPathAddLineToPoint(path, nil, maxX - arrowHeight, (minY+maxY)/2 + arrowWidth/2)
//arrow end
CGPathAddLineToPoint(path, nil, maxX - arrowHeight, maxY - corner)
CGPathAddArcToPoint(path, nil, maxX - arrowHeight, maxY, maxX - arrowHeight - corner, maxY, corner)
CGPathAddLineToPoint(path, nil, corner, maxY)
CGPathAddArcToPoint(path, nil, minX, maxY, minX, maxY - corner, corner)
CGPathAddLineToPoint(path, nil, minX, corner)
CGPathAddArcToPoint(path, nil, minX, minY, minX + corner, minY, corner);
CGPathCloseSubpath(path)
return path
}

private func rightBottomPath() -> CGPath {
let corner: CGFloat = self.corner
let width = CGRectGetWidth(self.frame)
Expand Down

0 comments on commit 689e90b

Please sign in to comment.