如何在UIView上设置平滑的边框颜色?

时间:2012-06-29 13:25:50

标签: ios uiview core-graphics quartz-graphics

我在View Controller上添加了一个UIView。最初我在UIView上设置了边框颜色,现在它看起来很平滑。但是在通过Gesture识别器缩放View时,边框颜色会像素化。

这里的初始代码

- (id)initWithImage:(UIImage*)image delegate:(id)delegat{
    if ( (self = [super initWithImage:image]) ) {
        self.layer.masksToBounds = YES;
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width+1, self.frame.size.height+1);
        self.contentMode = UIViewContentModeCenter;
        ratio = image.size.height / image.size.width;
        itemType = 2;
        self.userInteractionEnabled =TRUE;
        self.delegate = delegat;
        self.layer.borderColor = [UIColor redColor].CGColor;
        self.layer.borderWidth = 4;
        scaleFactor = 1.0;
        rotateAngle = 0.0;
        fontSize = 22;
        self.contentScaleFactor = 1.0;
        if(image != nil){
            frameWidth = image.size.width;
            frameHeight = image.size.height;
        }else{
            frameWidth = self.frame.size.width;
            frameHeight = self.frame.size.height;    
        }

        [self addGestureRecognizers];
    }    
    return self;
}

enter image description here

这里是旋转代码的代码

- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer{


    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        // To undo the action : store information
        [[UIAppDelegate viewController] storeAction:CHUndoActionTypeRotate item:self];

        [gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
        [gestureRecognizer setRotation:0];
    }

}

这是我在旋转视图时得到的

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个看似很常见的问题。你想要的是边界是消除锯齿的。 Here is one post使用解决方案和here is another

基本上你可以使用UIImageView并在图像周围添加一个像素透明边框,这样实际边框就会消除锯齿。