iphone - 使用 touchesMoved 缩放 :

标签 iphone objective-c ios touchesmoved uibezierpath

我有一个 pie chart这是使用 UIBezierPath 制作的。我现在需要那些单独的路径(饼图)是可扩展的。我相信您需要一个 View 才能使用收缩缩放,所以我认为 touchesMoved: 是可行的方法(除非有解决方法)。

如有任何建议或帮助,我们将不胜感激!

更新/进度代码

MySliceClass.m

+ (UIBezierPath *)sliceRadius:(float)radius andStartingAngle:(float)startingAngle andFinishingAngle:(float)finishingAngle
{
  static UIBezierPath *path = nil;
  path = [UIBezierPath bezierPath];
  CGPoint center = {300,300};
  [path moveToPoint:center];
  [path addArcWithCenter:center radius:radius startAngle:radians(startingAngle) endAngle:radians(finishingAngle) clockwise:YES];
  [path closePath];
  path.lineWidth = 1;

  [[UIColor redColor] setFill];
  [path fill];

  return path;
}

MySliceView.m

- (void)drawRect:(CGRect)rect 
{
  NSArray *arrayOfSlices = [NSArray arrayWithObjects:
                            slice01 = [WordplaySlice sliceRadius:200 andStartingAngle:0.5 andFinishingAngle:29.5],
                            slice02 = [WordplaySlice sliceRadius:200 andStartingAngle:30.5 andFinishingAngle:59.5],
                            slice03 = [WordplaySlice sliceRadius:200 andStartingAngle:60.5 andFinishingAngle:89.5],
                            slice04 = [WordplaySlice sliceRadius:200 andStartingAngle:90.5 andFinishingAngle:119.5],
                            slice05 = [WordplaySlice sliceRadius:200 andStartingAngle:120.5 andFinishingAngle:149.5],
                            slice06 = [WordplaySlice sliceRadius:200 andStartingAngle:150.5 andFinishingAngle:179.5],
                            slice07 = [WordplaySlice sliceRadius:200 andStartingAngle:180.5 andFinishingAngle:209.5],
                            slice08 = [WordplaySlice sliceRadius:200 andStartingAngle:210.5 andFinishingAngle:239.5],
                            slice09 = [WordplaySlice sliceRadius:200 andStartingAngle:240.5 andFinishingAngle:269.5],
                            slice10 = [WordplaySlice sliceRadius:200 andStartingAngle:270.5 andFinishingAngle:299.5],
                            slice11 = [WordplaySlice sliceRadius:200 andStartingAngle:300.5 andFinishingAngle:329.5],
                            slice12 = [WordplaySlice sliceRadius:200 andStartingAngle:330.5 andFinishingAngle:359.5], nil];                             
}

最佳答案

我认为如果为每个切片创建一个 View 并使用 UIPinchGestureRecognizer,您会发现它更容易。方法如下。

首先,我们需要一个绘制一个切片的 UIView 子类。它还应该覆盖 pointInside:withEvent: 以忽略落在切片之外的触摸(即使触摸在 View 的矩形边界内)。

所以我们将创建一个名为 SliceView 的类。它使用 CAShapeLayer 来绘制切片:

@interface SliceView : UIView

@property (nonatomic) CGFloat padding;
@property (nonatomic) CGFloat startRadians;
@property (nonatomic) CGFloat endRadians;
@property (nonatomic, strong) UIColor *fillColor;

@end

@implementation SliceView

@synthesize padding = _padding;
@synthesize startRadians = _startRadians;
@synthesize endRadians = _endRadians;
@synthesize fillColor = _fillColor;

我们通过覆盖 layerClass 方法告诉它使用 CAShapeLayer 而不是普通的 CALayer。我们还将添加一个方便的方法,将 View 的图层作为 CAShapeLayer 返回。

+ (Class)layerClass {
    return [CAShapeLayer class];
}

- (CAShapeLayer *)shapeLayer {
    return (CAShapeLayer *)self.layer;
}

我们将在 layoutSubviews 中计算切片的路径,因为只要 View 的大小发生变化,它就会收到 layoutSubviews 消息。

我们将布置每个切片 View 以覆盖整个饼图,但只绘制饼图的楔形部分。每个切片的框架将覆盖整个屏幕(如果饼图是全屏的)。这意味着切片 View 知道其圆弧的中心位于其边界的中心。但随后我们使用一点三角函数来填充相邻切片之间的填充。

我们还调整了图层的 anchor ;这是在缩放或旋转图层时不会移动的图层中的点。我们希望 anchor 位于离中心最近的切片角上。

- (void)layoutSubviews {
    CAShapeLayer *layer = self.shapeLayer;
    CGRect bounds = self.bounds;
    CGFloat radius = MIN(bounds.size.width, bounds.size.height) / 2 - 2 * _padding;
    CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
    CGFloat sine = sinf((_startRadians + _endRadians) * 0.5f);
    CGFloat cosine = cosf((_startRadians + _endRadians) * 0.5f);
    center.x += _padding * cosine;
    center.y += _padding * sine;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:center];
    [path addArcWithCenter:center radius:radius startAngle:_startRadians endAngle:_endRadians clockwise:YES];
    [path closePath];
    layer.path = path.CGPath;

    // Move my anchor point to the corner of my path so scaling will leave the corner in the same place.
    CGPoint cornerInSuperview = [self convertPoint:center toView:self.superview];
    layer.anchorPoint = CGPointMake(center.x / bounds.size.width, center.y / bounds.size.height);
    self.center = cornerInSuperview;
}

当任何与切片相关的 View 属性发生变化时,我们需要重新计算概述切片的路径。当切片的填充颜色发生变化时,我们需要将该变化传递给图层。所以我们将覆盖属性 setter 。

- (void)setPadding:(CGFloat)padding {
    _padding = padding;
    [self setNeedsLayout];
}

- (void)setStartRadians:(CGFloat)startRadians {
    _startRadians = startRadians;
    [self setNeedsLayout];
}

- (void)setEndRadians:(CGFloat)endRadians {
    _endRadians = endRadians;
    [self setNeedsLayout];
}

- (void)setFillColor:(UIColor *)color {
    _fillColor = color;
    self.shapeLayer.fillColor = color.CGColor;
}

最后,我们重写 pointInside:withEvent:,这样如果触摸实际位于切片路径内,则 HitTest 只会将触摸分配给切片 View 。这一点很关键,因为所有切片 View 都将有一个覆盖整个屏幕的框架。

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return CGPathContainsPoint(self.shapeLayer.path, NULL, point, NO);
}

@end

现在我们有了一个方便的 SliceView 类,我们可以用它来绘制带有可缩放切片的饼图。很难将两根手指放入 iPhone 屏幕上的一个切片中,因此我们让用户点击一个切片来选择它,然后在任意位置捏合以缩放所选切片。 (此接口(interface)还使其可在模拟器中进行测试。)

@implementation ViewController {
    __weak SliceView *_selectedSlice;
}

我们将用红色绘制未选定的切片,用蓝色绘制选定的切片。

+ (UIColor *)unselectedSliceFillColor {
    return UIColor.redColor;
}

+ (UIColor *)selectedSliceFillColor {
    return UIColor.blueColor;
}

当用户点击切片时,我们需要更改先前选择和新选择的颜色,并记录新选择。

- (IBAction)sliceWasTapped:(UITapGestureRecognizer *)tapper {
    _selectedSlice.fillColor = self.class.unselectedSliceFillColor;
    _selectedSlice = (SliceView *)tapper.view;
    _selectedSlice.fillColor = self.class.selectedSliceFillColor;
}

当用户捏合时,我们调整所选切片的变换(如果有的话)。

- (IBAction)pinched:(UIPinchGestureRecognizer *)pincher {
    if (!_selectedSlice)
        return;
    CGFloat scale = pincher.scale;
    pincher.scale = 1;
    _selectedSlice.transform = CGAffineTransformScale(_selectedSlice.transform, scale, scale);
}

最后,我们需要实际创建切片 View 和手势识别器。我们为每个切片创建一个点击识别器,并为背景 View 创建一个“全局”捏合识别器。

- (void)viewDidLoad {
    static int const SliceCount = 12;
    CGRect bounds = self.view.bounds;
    for (int i = 0; i < SliceCount; ++i) {
        SliceView *slice = [[SliceView alloc] initWithFrame:bounds];
        slice.startRadians = 2 * M_PI * i / SliceCount;
        slice.endRadians = 2 * M_PI * (i + 1) / SliceCount;
        slice.padding = 4;
        slice.fillColor = self.class.unselectedSliceFillColor;
        slice.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:slice];

        UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliceWasTapped:)];
        [slice addGestureRecognizer:tapper];
    }

    UIPinchGestureRecognizer *pincher = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinched:)];
    [self.view addGestureRecognizer:pincher];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

这是它的样子:

SliceView demo screen shot

你可以在这里下载我的测试项目:http://dl.dropbox.com/u/26919672/pie.zip

更新

针对您询问限制比例的评论,我建议向 SliceView 添加更多属性:

@property (nonatomic) CGFloat minScale;
@property (nonatomic) CGFloat maxScale;
@property (nonatomic) CGFloat scale;

重要:您需要在 initWithFrame:initWithCoder: 中将所有三个属性初始化为 1。

然后,实现 scale setter 以实际执行限制并设置比例:

- (void)setScale:(CGFloat)scale {
    _scale = MAX(minScale, MIN(scale, maxScale));
    self.transform = CGAffineTransformMakeScale(_scale, _scale);
}

pinched: 中,您更新 View 的 scale 属性,而不是直接设置 View 的 transform 属性:

- (IBAction)pinched:(UIPinchGestureRecognizer *)pincher {
    if (!_selectedSlice)
        return;
    CGFloat scale = pincher.scale;
    pincher.scale = 1;
    _selectedSlice.scale = _selectedSlice.scale * scale;
}

关于iphone - 使用 touchesMoved 缩放 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9354530/

相关文章:

ios - 在 swrevealviewcontroller 中显示导航栏

ios - ios 分段传输到服务器时图像文件如何损坏

iphone - 如何识别应用程序内App Store的国家/地区

iphone - iOS - UI 自动化通过可访问性标签获取文本字段?

iphone 6 的 iOS 背景图片

ios - 仅从 UIView 的 3 个边绘制阴影

ios - 从 'CGFloat *' 分配给 'float *'(又名 'int')的指针转换不兼容整数

iphone - cocos2d-iphone 中的 CCMoveBy

iphone - 核心数据错误:由于未捕获的异常 'NSInvalidArgumentException'而终止应用程序,原因: 'Can'对对象0进行正则表达式匹配

ios - 实现 MTLModel 显示解析 json 字典时出错