2012-08-23 3 views
3

둥근 모서리가있는 사각형을 그리고 검은 색으로 채우기 위해 간단한 UIView 확장에서 drawRect 메서드를 재정의하고 터치하여 크기를 조정하도록보기를 애니메이션화하려고했습니다. 상황에 따라 전체 크기가 동일하게 유지되는 것처럼 크기를 변경하면 도면이 완전히 변형됩니다. drawRect는 애니메이션 중에는 호출되지 않습니다. 그러나 애니메이션 뒤에 그림을 그리라고 지시하면 올바르게 그려집니다.iOS UIView drawRect 크기 조정 애니메이션

드로잉을 변형시키지 않고 리사이징을 어떻게 애니메이트 할 수 있습니까?

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGFloat radius = rect.size.height * 0.5f; 

    CGMutablePathRef path = CGPathCreateMutable(); 

    CGPathMoveToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMinY(rect)); 
    CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect), 
        CGRectGetMaxX(rect), CGRectGetMaxY(rect), radius); 
    CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect), 
        CGRectGetMinX(rect), CGRectGetMaxY(rect), radius); 
    CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect), 
        CGRectGetMinX(rect), CGRectGetMinY(rect), radius); 
    CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect), 
        CGRectGetMaxX(rect), CGRectGetMinY(rect), radius); 
    CGPathCloseSubpath(path); 

    CGContextSaveGState(context); 
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
    CGContextAddPath(context, path); 
    CGContextFillPath(context); 
    CGContextRestoreGState(context); 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [UIView animateWithDuration:1.f animations:^{ 
    self.frame = CGRectInset(self.frame, 0.f, -100.f); 
    }]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [UIView animateWithDuration:1.f animations:^{ 
    self.frame = CGRectInset(self.frame, 0.f, +100.f); 
    }]; 
} 
+0

I 추측을 가져온 http://www.nomadplanet.fr/2010/11/animate-calayer- coreanimation과 함께 사용자 정의 속성/ – Jonny

답변

2

당신은 명시 적으로의 drawRect를 재정의 할 필요가 없습니다 UIView의에 둥근 모서리를합니다. UIView의 레이어 속성을 변경하는 간단한 방법이 있습니다. 애니메이션 중에 뷰와 경계가 정확하게 그려집니다.

// Make your view background color black 
myView.backgroundColor = [UIColor blackColor]; 

// The following will allow you to change the color/border width/ corner radius of your UIView 
myView.layer.borderColor = [UIColor whiteColor].CGColor; 
myView.layer.borderWidth = kDefaultBorderWidth; 
myView.layer.cornerRadius = kDefaultBorderRadius; 

당신이 당신의하는 .m 파일에 #import <QuartzCore/QuartzCore.h> 포함되어 있는지 확인하고 여기에 QuartzCore.framework

+1

동의 함 -'myView.clipsToBounds = YES; '를 설정했는지 확인하십시오. – Ander

+0

그렇습니다. 그러나 둥근 사각형에 사용자 지정 효과를 그리려면 어떻게해야합니까? – Avahi

+0

어떤 다른 효과를 달성하고 싶습니다 - 내 대답은 귀하의 질문과 관련이 있습니다 ... – tiguero