티스토리 뷰
UIView 나 UIButton 등에 곡선을 그려줘야 할 경우가 있다.
그럴 경우에는 UIVIewController 내부에 직접
func imageShape(_ button : UIButton) {
let path1 = UIBezierPath(roundedRect: button.bounds, byRoundingCorners: [.topLeft, .bottomLeft] , cornerRadii: CGSize(width: 10, height: 10))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = button.bounds
maskLayer1.path = path1.cgPath
button.layer.mask = maskLayer1
}
이런 식으로 선언해주고 function을 불러주어도 실행된다.
이 때 , 시뮬레이터로 실행한 결과
byRoundingConers 안에 topLeft , bottomLeft 넣어준대로 오른쪽 밑과 왼쪽 위가 곡선을 이루고 있는것이 보인다.
나는 처음에 저런식으로 사용하였다가 , viewcontroller 내의 코드를 조금이라도 줄이기 위해서
swift 파일을 새로 만들어서 draw 함수에 선언해주었다.
import UIKit
class RightUpperBorderButton: UIButton {
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
let path1 = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [.topRight], cornerRadii: CGSize(width: 10, height: 10))
let maskLayer1 = CAShapeLayer()
maskLayer1.frame = self.bounds
maskLayer1.path = path1.cgPath
self.layer.mask = maskLayer1
}
}
그런 다음
storyboard 에서
이렇게 클래스 설정만 해주면 적용이 다 된 것이다.
시뮬로 돌려본 결과
오른쪽 위가 곡선이 입혀진게 보인다.
'Swift' 카테고리의 다른 글
Navigation Controller 사용시에 두번 POP 하는 방법 (1) | 2018.09.17 |
---|---|
UITableView 위로 Scroll 한 다음 UITableVIew Update 하기 (0) | 2018.08.20 |
UIRefreshControl : 스크롤 내려서 Reload, Refresh 하기 (Scroll Down) (0) | 2018.08.11 |
UIActivityVIewController - 공유하기버튼 (0) | 2018.08.02 |
iOS Status Bar Background Color Change (0) | 2018.07.23 |
- Total
- Today
- Yesterday