티스토리 뷰
public extension UIView {
private static let kLayerNameGradientBorder = "GradientBorderLayer"
func setGradientBorder(
width: CGFloat,
colors: [UIColor],
startPoint: CGPoint = CGPoint(x: 0.5, y: 0),
endPoint: CGPoint = CGPoint(x: 0.5, y: 1)
) {
let existedBorder = gradientBorderLayer()
let border = existedBorder ?? CAGradientLayer()
border.frame = bounds
border.colors = colors.map { return $0.cgColor }
border.startPoint = startPoint
border.endPoint = endPoint
let mask = CAShapeLayer()
mask.path = UIBezierPath(roundedRect: bounds, cornerRadius: 0).cgPath
mask.fillColor = UIColor.clear.cgColor
mask.strokeColor = UIColor.white.cgColor
mask.lineWidth = width
border.mask = mask
let exists = existedBorder != nil
if !exists {
layer.addSublayer(border)
}
}
func removeGradientBorder() {
self.gradientBorderLayer()?.removeFromSuperlayer()
}
private func gradientBorderLayer() -> CAGradientLayer? {
let borderLayers = layer.sublayers?.filter { return $0.name == UIView.kLayerNameGradientBorder }
if borderLayers?.count ?? 0 > 1 {
fatalError()
}
return borderLayers?.first as? CAGradientLayer
}
}
사용법
ex) self.setGradientBorder(width: 2.0, colors: [UIColor.systemGray2,UIColor.systemGray5])
'iOS 개발 > Swift Code' 카테고리의 다른 글
(swift) view.isUserInteractionEnabled 활용 (0) | 2021.11.02 |
---|---|
(swift) collectionview : dequeueReusableCell withReuseIdentifier사용시 주의점(셀 선택 시 다른 셀도 선택되는 오류) (0) | 2021.11.02 |
(swift) UIView 테두리 설정하기. (테두리 두께, 곡률, 색, 그림자) (0) | 2021.09.05 |
- Total
- Today
- Yesterday
- 알고리즘
- ios simulator
- ios
- swiftc
- 주입
- 학교 과제
- 의존관계역전법칙
- 클린 코드 줄거리
- 클린 코드 정리
- clean code
- 메모리 순환참조
- dfs
- 전처리기
- SwiftUI
- 링커
- 의존성
- CLANG
- XcodeBuildSystem
- 단어변환
- 생명 주기
- clean code 정리
- 면접질문
- Swift
- XCFramework
- 클린 코드
- BFS
- 프로그래머스
- 이분탐색
- 순환참조
- 여행경로
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |