在缩放时,多个UIViews像素化

时间:2014-01-26 15:37:28

标签: ios objective-c uiview uibezierpath uipinchgesturerecognizer

我有两个UIViews(BlueView和RedView)绘制UIBeizerPath(它必须是uibeizarpath)。

我的MainView初始化两个视图并控制缩放功能。

我可以让视图进行缩放,但是,当它们缩放时,UIBeizerPaths会像素化。我尝试过使用[viewname].layer.contentsScale = [[UIScreen mainScreen] scale] * currentScale;,但这不起作用。

非常感谢任何帮助!

通过Photoshop的魔力,这里有一些我想要完成的事情的插图。

视图看起来没有缩放:

Non Zoomed

放大的结果:

Bad Zoom

缩放时所需的结果:

Desired Zoom

有什么建议吗?非常感谢。这是我的代码:

MainView.h

#import <UIKit/UIKit.h>
#import “RedLine.h”
#import “BlueLine.h

@interface MainView : UIViewController {
    CGRect screenRect;

    //ZOOM
    CGFloat zoomScale;
    CGFloat _previousScale;
    CGPoint _originalCenter; 

RedLine * redLine;
BlueLine * blueLine;
}
@end

MainView.m

#import "MainView.h"
#import "BlueLine.h"
#import "RedLine.h"

// ZOOM SCALE
#define MAX_SCALE 4.0
#define MIN_SCALE 1.0


@interface MainView ()

@end

@implementation MainView

- (void)viewDidLoad
{
    [super viewDidLoad];
    screenRect = [[UIScreen mainScreen] bounds];

    zoomScale = 1.0;


    //Draw the lines
    blueLine = [[BlueLine alloc] initWithFrame:self.view.frame];
    [self.view addSubview:blueLine];

    redLine = [[RedLine alloc] initWithFrame:self.view.frame];
    [self.view addSubview:redLine];



    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoomTool:)];
    [self.view addGestureRecognizer:pinchGesture];

}

#pragma mark - ZOOM 

//ZOOM VIEW
- (void) zoomTool:(UIPinchGestureRecognizer *)recognizer
{
    NSLog(@"Pinch scale: %f", recognizer.scale);
    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        _previousScale = zoomScale;
    }

    CGFloat currentScale = MAX(MIN([recognizer scale] * zoomScale, MAX_SCALE), MIN_SCALE);
    CGFloat scaleStep = currentScale / _previousScale;
    [redLine setTransform: CGAffineTransformScale(redLine.transform, scaleStep, scaleStep)];
    redLine.layer.contentsScale = [[UIScreen mainScreen] scale] * currentScale;
    [blueLine setTransform: CGAffineTransformScale(blueLine.transform, scaleStep, scaleStep)];
    blueLine.layer.contentsScale = [[UIScreen mainScreen] scale] * currentScale;

    _previousScale = currentScale;

    if ([recognizer state] == UIGestureRecognizerStateEnded ||
        [recognizer state] == UIGestureRecognizerStateCancelled ||
        [recognizer state] == UIGestureRecognizerStateFailed) {
        // Gesture can fail (or cancelled?) when the notification and the object is dragged simultaneously
        zoomScale = currentScale;
    }


}
@end

BlueLine.m

#import "BlueLine.h"

@implementation BlueLine

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    UIBezierPath *thisLine = [UIBezierPath bezierPath];

    [thisLine moveToPoint:CGPointMake(100, 100)];
    [thisLine addLineToPoint:CGPointMake(250, 250)];
    thisLine.lineWidth = 10;
    [[UIColor blueColor] setStroke];
    [thisLine stroke];

}
@end

RedLine.m

#import "RedLine.h"

@implementation RedLine

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    UIBezierPath *thisLine = [UIBezierPath bezierPath];

    [thisLine moveToPoint:CGPointMake(300, 300)];
    [thisLine addLineToPoint:CGPointMake(100, 450)];
    thisLine.lineWidth = 10;
    [[UIColor redColor] setStroke];
    [thisLine stroke];

}
@end

1 个答案:

答案 0 :(得分:0)

变换只会拉伸视图,因此会按照以下步骤进行像素化处理。

当缩放发生时调用视图的setNeedsLayout方法。并按比例增加lineWidth乘以原始宽度。它将完美绘制。

在您的观看次数中添加新的公共属性。以及缺少的行。

@property CGFloat zoomScale;

-(void)viewDidLoad{
 zoomScale=1.0;
}

-(void)drawRect{
    thisLine.lineWidth = 10*zoomScale;
}

所以只要缩放发生,

blueView.zoomScale =zoomScale;
[blueView setNeedsDisplay];

完整代码(红线)

- (void) zoomTool:(UIPinchGestureRecognizer *)recognizer
{
    NSLog(@"Pinch scale: %f", recognizer.scale);
    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        _previousScale = zoomScale;
    }

    CGFloat currentScale = MAX(MIN([recognizer scale] * zoomScale, MAX_SCALE), MIN_SCALE);
    redLine.scale=currentScale;
    [redLine setNeedsDisplay];
    _previousScale = currentScale;

    if ([recognizer state] == UIGestureRecognizerStateEnded ||
        [recognizer state] == UIGestureRecognizerStateCancelled ||
        [recognizer state] == UIGestureRecognizerStateFailed) {
        // Gesture can fail (or cancelled?) when the notification and the object is dragged simultaneously
        zoomScale = currentScale;
    }
}

<强> RedLine.h

#import <UIKit/UIKit.h>

@interface RedLine : UIView
@property CGFloat scale;
@end

<强> RedLine.m

#import "RedLine.h"

@implementation RedLine
@synthesize scale =scale;
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        scale=1.0f;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {


    [self setBackgroundColor:[UIColor clearColor]];

    CGPoint p1 =CGPointMake(50, 50);
    CGPoint p2 =CGPointMake(300, 300);

    CGFloat offsetX =self.center.x *(scale-1);
    CGFloat offsetY =self.center.y *(scale-1);

    p1.x =p1.x*scale -offsetX;
    p1.y =p1.y*scale -offsetY;

    p2.x =p2.x*scale -offsetX;
    p2.y =p2.y*scale -offsetY;


    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetLineWidth(context, 10*scale);

    CGContextMoveToPoint(context, p1.x, p1.y);
    CGContextAddLineToPoint(context, p2.x, p2.y);

    CGContextStrokePath(context);

}
@end