iOS 개발 방법 은 View 에 지정 한 위치의 경계선 을 추가 하 는 방법 입 니 다.
본 고 는 주로 iOS 가 View 에 지 정 된 위치 경계선 을 추가 하 는 방법 에 관 한 내용 을 소개 하고 참고 학습 을 제공 합 니 다.다음은 더 이상 할 말 이 없 으 니 상세 한 소 개 를 해 보 겠 습 니 다.
약간 봉 인 했 습 니 다.View 에 지정 한 위치의 경계선 을 추가 하 였 습 니 다.그 중에서 변위 매 거 진 사용 으로 친구 들 에 게 물 어 보 았 습 니 다.드디어 해결 되 었 습 니 다.
예제 코드
포장 1:직접 포장 하 는 방법
/// ( )
typedef NS_ENUM(NSInteger, UIBorderSideType) {
UIBorderSideTypeAll = 0,
UIBorderSideTypeTop = 1 << 0,
UIBorderSideTypeBottom = 1 << 1,
UIBorderSideTypeLeft = 1 << 2,
UIBorderSideTypeRight = 1 << 3,
};
/**
view
@param originalView view
@param color
@param borderWidth
@param borderType : UIBorderSideTypeTop|UIBorderSideTypeBottom
@return view
*/
- (UIView *)borderForView:(UIView *)originalView color:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType {
if (borderType == UIBorderSideTypeAll) {
originalView.layer.borderWidth = borderWidth;
originalView.layer.borderColor = color.CGColor;
return originalView;
}
///
UIBezierPath * bezierPath = [UIBezierPath bezierPath];
///
if (borderType & UIBorderSideTypeLeft) {
///
[bezierPath moveToPoint:CGPointMake(0.0f, originalView.frame.size.height)];
[bezierPath addLineToPoint:CGPointMake(0.0f, 0.0f)];
}
///
if (borderType & UIBorderSideTypeRight) {
///
[bezierPath moveToPoint:CGPointMake(originalView.frame.size.width, 0.0f)];
[bezierPath addLineToPoint:CGPointMake( originalView.frame.size.width, originalView.frame.size.height)];
}
/// top
if (borderType & UIBorderSideTypeTop) {
/// top
[bezierPath moveToPoint:CGPointMake(0.0f, 0.0f)];
[bezierPath addLineToPoint:CGPointMake(originalView.frame.size.width, 0.0f)];
}
/// bottom
if (borderType & UIBorderSideTypeBottom) {
/// bottom
[bezierPath moveToPoint:CGPointMake(0.0f, originalView.frame.size.height)];
[bezierPath addLineToPoint:CGPointMake( originalView.frame.size.width, originalView.frame.size.height)];
}
CAShapeLayer * shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = color.CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
///
shapeLayer.path = bezierPath.CGPath;
///
shapeLayer.lineWidth = borderWidth;
[originalView.layer addSublayer:shapeLayer];
return originalView;
}
포장 2:분류 로 포장내용
#import <UIKit/UIKit.h>
typedef NS_OPTIONS(NSUInteger, UIBorderSideType) {
UIBorderSideTypeAll = 0,
UIBorderSideTypeTop = 1 << 0,
UIBorderSideTypeBottom = 1 << 1,
UIBorderSideTypeLeft = 1 << 2,
UIBorderSideTypeRight = 1 << 3,
};
@interface UIView (BorderLine)
- (UIView *)borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType;
@end
내용
#import "UIView+BorderLine.h"
@implementation UIView (BorderLine)
- (UIView *)borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType {
if (borderType == UIBorderSideTypeAll) {
self.layer.borderWidth = borderWidth;
self.layer.borderColor = color.CGColor;
return self;
}
///
if (borderType & UIBorderSideTypeLeft) {
///
[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.f, 0.f) toPoint:CGPointMake(0.0f, self.frame.size.height) color:color borderWidth:borderWidth]];
}
///
if (borderType & UIBorderSideTypeRight) {
///
[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(self.frame.size.width, 0.0f) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
}
/// top
if (borderType & UIBorderSideTypeTop) {
/// top
[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, 0.0f) toPoint:CGPointMake(self.frame.size.width, 0.0f) color:color borderWidth:borderWidth]];
}
/// bottom
if (borderType & UIBorderSideTypeBottom) {
/// bottom
[self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, self.frame.size.height) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
}
return self;
}
- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {
///
UIBezierPath * bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:p0];
[bezierPath addLineToPoint:p1];
CAShapeLayer * shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = color.CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
///
shapeLayer.path = bezierPath.CGPath;
///
shapeLayer.lineWidth = borderWidth;
return shapeLayer;
}
@end
사용법:
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(80.0f, 80.0f, 200.0f, 100.0f)];
testView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:testView];
[self borderForView:testView color:[UIColor redColor] borderWidth:1.0f borderType:UIBorderSideTypeTop | UIBorderSideTypeBottom];
효과:부족 한 점,경계선 이 너무 넓 으 면 접경 에 흰색 이 남는다.
ps:주의:먼저 view 를 부모 view 에 불 러 와 야 합 니 다.[self.view addSubview:testView];그리고 테두리 설정 하기;그렇지 않 으 면 소 용이 없 을 수도 있다.
총결산
이상 은 이 글 의 전체 내용 입 니 다.본 논문 의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가치 가 있 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 댓 글 을 남 겨 주 셔 서 저희 에 대한 지지 에 감 사 드 립 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.