내 iPad 앱에서 사용자가 버튼을 클릭하면 기본보기에 두 번째보기가 표시됩니다. 새보기는 첫 번째보기보다 작아지고 배경이 표시되면 어두워집니다. 새 뷰의 상단 두 모서리가 둥글게 보이도록하고 싶지만 cornerRadius를 사용하면 모든 뷰가 둥글게 설정됩니다. 두 모퉁이를 어떻게 둥글게 만들 수 있습니까?둥근 모서리가 두 개입니까?
답변
drawRect :에서이 작업을 수행해야합니다. 실제로 고전 addRoundedRectToPath 수정 :이 비트 맵을 받아 반올림 있도록 모서리가 요청 :
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float radius, UIImageRoundedCorner cornerMask)
{
CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
if (cornerMask & UIImageRoundedCornerTopLeft) {
CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius,
radius, M_PI, M_PI/2, 1);
}
else {
CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height);
CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y + rect.size.height);
}
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height);
if (cornerMask & UIImageRoundedCornerTopRight) {
CGContextAddArc(context, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height - radius, radius, M_PI/2, 0.0f, 1);
}
else {
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height - radius);
}
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
if (cornerMask & UIImageRoundedCornerBottomRight) {
CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius,
radius, 0.0f, -M_PI/2, 1);
}
else {
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, rect.origin.y);
}
CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
if (cornerMask & UIImageRoundedCornerBottomLeft) {
CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius,
-M_PI/2, M_PI, 1);
}
else {
CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + radius);
}
CGContextClosePath(context);
}
이 (나는 이미지를이 일을했기 때문에 나는 그것을 UIImageRoundedCorner라고하지만, 당신이 그것을 호출 할 수있는 비트 마스크를 무슨 일이든) 그리고 둥근 원하는 모퉁이를 기반으로 경로를 만듭니다. 내 코드가 정확히의 drawRect에서 사용하도록 설정되지 않도록, 내가 :,, UIImages을 위해이 일을했다 말했듯이
CGContextBeginPath(context);
addRoundedRectToPath(context, rect, radius, yourMask);
CGContextClosePath(context);
CGContextClip(context);
하지만 꽤 쉽게해야합니다 : 그럼 당신의 drawRect에서보기로 해당 경로를 적용 그것을 적응 시키십시오. 기본적으로 경로를 만든 다음 컨텍스트를 클리핑합니다.
편집 :는 비트 마스크 부분을 설명하기 위해, 그냥 열거입니다 : 이런 식으로
typedef enum {
UIImageRoundedCornerTopLeft = 1,
UIImageRoundedCornerTopRight = 1 << 1,
UIImageRoundedCornerBottomRight = 1 << 2,
UIImageRoundedCornerBottomLeft = 1 << 3
} UIImageRoundedCorner;
할 수 있습니다 또는 열거의 각 구성원이를 나타 내기 때문에, 코너를 식별하는 비트 마스크를 형성하기 위해 함께 일 비트 마스크에서 2의 다른 힘.
훨씬 나중에 편집 : 나는이 사용 UIBezierPath
의 bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
할 수있는 쉬운 방법을 발견했습니다. 컨텍스트에서 베 지어 경로 객체의 CGPath
을 사용하기 만하면됩니다.
// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds
byRoundingCorners:UIRectCornerTopLeft| UIRectCornerTopRight
cornerRadii:CGSizeMake(10.0, 10.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imageView.bounds;
maskLayer.path = maskPath.CGPath;
// Set the newly created shape layer as the mask for the image view's layer
imageView.layer.mask = maskLayer;
상단 둥근 모서리를 사용하는 것이 좋습니다. Round two corners in UIView
그게 전부 잘 작동합니다. 감사합니다. –
그게 효과가 있지만, 전체 이미지 뷰 영역에서 오프 스크린 렌더링을 일으켜서 애니메이션이 뒤죽박죽이됩니다. – Philip007
제가이 주제에서 찾은 최고의 답을 찾았습니다! – PaperThick
Tomek Kuźma 님이 완성한 멋진 작품. 다음은 요구 사항에 맞는 새로운 TKRoundedView 클래스입니다.
이 매개 변수를 통해서만 요구 사항을 충족시킬 수 있습니다.
TKRoundedView *view = [[TKRoundedView alloc] initWithFrame:frame];
view.roundedCorners = TKRoundedCornerTopLeft | TKRoundedCornerTopRight;
그러나 다음과 같은 추가 기능도 제공합니다.
는TKRoundedView *view = [[TKRoundedView alloc] initWithFrame:frame];
view.roundedCorners = TKRoundedCornerTopLeft
view.borderColor = [UIColor greenColor];
view.fillColor = [UIColor whiteColor];
view.drawnBordersSides = TKDrawnBorderSidesLeft | TKDrawnBorderSidesTop;
view.borderWidth = 5.0f;
view.cornerRadius = 15.0f;
^너무 좋아! 자동 레이아웃과 잘 작동합니다. –
- 1. 아래쪽 모서리가 둥근 NSWindow
- 2. 모서리가 둥근 UIToolbar
- 3. 안드로이드에 모서리가 둥근 이미지
- 4. 테이블에 모서리가 둥근 모서리가 보이지 않음
- 5. MultipleDetailViews 샘플 코드에서 모서리가 둥근 모서리가 사라졌습니다.
- 6. 캔버스의 이미지에 둥근 모서리가 있습니다.
- 7. 목록보기 및 둥근 모서리가 작동하지 않습니다.
- 8. PHP를 사용하여 이미지에 둥근 모서리가 있습니까?
- 9. NSBezierPath 둥근 사각형의 모서리가 맞지 않습니다.
- 10. IE에서 입력 요소의 모서리가 둥근 경우
- 11. NSView 하위 모서리 둥근 모서리가 작동하지 않습니다.
- 12. 모서리가 둥근 NSImageView를 만드는 방법은 무엇입니까?
- 13. UIButton에 둥근 모서리가 하나만있는 방법이 있습니까?
- 14. 어떻게 모퉁이가 둥근 모서리가 standarted가 아니게 만드나요?
- 15. has_one 외래 키가 두 개입니까?
- 16. UINavigationBar 둥근 두 모서리
- 17. IE7에서 접을 수있는 div에 둥근 모서리가 엉망이 됨
- 18. AJAX 호출을하는 동안 Jquery 둥근 모서리가 작동하지 않습니다. 모든 아이디어
- 19. WPF에서 모서리가 둥근 대시 테두리를 만드는 방법은 무엇입니까?
- 20. 상위 DIV의 둥근 모서리가 하위 DIV에 영향을 미치지 않습니다.
- 21. SVG 스트로크가있는 폴리 라인 - 라인 결합 : 둥근 모서리가 보이지 않음
- 22. css1을 사용하여 모서리가 둥근 컨테이너를 만드는 방법은 무엇입니까?
- 23. android에서 선택한 경우 listView의 둥근 모서리가 날카 로워집니다.
- 24. Silverlight에서 모서리가 둥근 비디오를 표시하는 가장 좋은 방법은 무엇입니까?
- 25. Silverlight에서 위쪽 (또는 아래쪽) 모서리가 둥근 사각형 모서리를 만드는 방법?
- 26. Rebol 2/Rebol 3의 모서리가 둥근 텍스트 필드?
- 27. 사이트 게시시 둥근 모서리가 제공되는 MVC 응용 프로그램 서식 파일
- 28. HTML, CSS : 둥근 모서리가 IE에서 투명한 배경이 아닙니다.
- 29. 모서리가 둥근 모서리, 테두리 및 그라디언트로 채우기 이미지 작성 C#
- 30. stretchableImageWithLeftCapWidth로 인해 모서리가 흐리게됩니다.
멋진 대답은 다음 링크를 체크 아웃하십시오! 그것을 구현하는 데 도움이 필요합니다. 우선, 어떻게 내 자신의 비트 마스크를 만들 수 있습니까? – Jumhyn
열거 형 설명을 편집 중입니다. – kevboh
내 대답은 당신이 원하는 둥근 모서리를 취할 수 있도록 만들어졌습니다. 맨 위 두 모서리 만 필요하고 다른 모서리는 필요 없으면 비트 마스크를 편집하고 둥근 모서리에 해당하는 두 섹션을 사용하면됩니다. – kevboh