1

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

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

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

任何帮助将不胜感激!

通过 Photoshop 的魔力,这里有一些我试图完成的插图。

视图看起来没有缩放:

非缩放

我放大的结果:

缩放不良

缩放时所需的结果:

所需的缩放

有什么建议么?将不胜感激。这是我的代码:

主视图.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

主视图.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

蓝线.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

红线.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
4

1 回答 1

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;
    }
}

红线.h

#import <UIKit/UIKit.h>

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

红线.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
于 2014-01-26T16:31:08.407 回答