Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用触摸移动缩放:_Iphone_Objective C_Ios_Touchesmoved_Uibezierpath - Fatal编程技术网

Iphone 使用触摸移动缩放:

Iphone 使用触摸移动缩放:,iphone,objective-c,ios,touchesmoved,uibezierpath,Iphone,Objective C,Ios,Touchesmoved,Uibezierpath,我有一个用UIBezierPath制作的。我现在需要这些单独的路径(饼图)是可伸缩的。我相信你需要一个视图来使用收缩缩放,所以我认为touchesMoved:是一个好方法(除非有解决办法) 任何建议或帮助都将不胜感激 更新/进度代码 MySliceClass.m + (UIBezierPath *)sliceRadius:(float)radius andStartingAngle:(float)startingAngle andFinishingAngle:(float)finishingAn

我有一个用UIBezierPath制作的。我现在需要这些单独的路径(饼图)是可伸缩的。我相信你需要一个视图来使用收缩缩放,所以我认为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];                             
}

首先,最好将切片存储在数组中

其次,定义类
myLiceClass
(可以从UIBezierPath继承)也是一个更好的主意。此类具有定义切片的属性:
startingAngle
endAngle

现在不仅改进了代码,而且调整切片的大小也变得更容易了

您必须将参数
radius
添加到类
MySliceClass
,并且每次触摸一个切片时,您都要更改其半径,并调用
[self-setNeedsDisplay]
,以便调用方法
drawRect

最后,还应该将初始化移到视图的初始化方法,因为这样做的方式是每次绘制视图时都创建新切片

编辑下面是一个示例实现

@implementation Slice // Subclass of NSObject

@synthesize radius, startAngle, endAngle, center;

- (void)draw
{
    UIBezierPath *path = nil;
    path = [UIBezierPath bezierPath];
    [path moveToPoint:center];
    [path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    [path closePath];
    path.lineWidth = 1;

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

@end

@实现视图
@合成切片;
-(id)initWithFrame:(CGRect)帧{
self=[super initWithFrame:frame];
如果(自我){
int numSlices=12;//您可以更改此
切片=[NSMutableArray];
对于(int i=0;i

我没有测试这段代码,所以可能有一些错误。但我希望您可以从这一点开始。

首先,最好将切片存储在数组中

其次,定义类
myLiceClass
(可以从UIBezierPath继承)也是一个更好的主意。此类具有定义切片的属性:
startingAngle
endAngle

现在不仅改进了代码,而且调整切片的大小也变得更容易了

您必须将参数
radius
添加到类
MySliceClass
,并且每次触摸一个切片时,您都要更改其半径,并调用
[self-setNeedsDisplay]
,以便调用方法
drawRect

最后,还应该将初始化移到视图的初始化方法,因为这样做的方式是每次绘制视图时都创建新切片

编辑下面是一个示例实现

@implementation Slice // Subclass of NSObject

@synthesize radius, startAngle, endAngle, center;

- (void)draw
{
    UIBezierPath *path = nil;
    path = [UIBezierPath bezierPath];
    [path moveToPoint:center];
    [path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    [path closePath];
    path.lineWidth = 1;

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

@end

@实现视图
@合成切片;
-(id)initWithFrame:(CGRect)帧{
self=[super initWithFrame:frame];
如果(自我){
int numSlices=12;//您可以更改此
切片=[NSMutableArray];
对于(int i=0;i

我没有测试这段代码,所以可能有一些错误。但我希望您可以从这一点开始。

我认为,如果您为每个切片创建一个视图,并使用
uipinchgestrerecognizer
,您会发现更容易。这是怎么做的

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

因此,我们将创建一个名为
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;
我们告诉它使用
CAShapeLayer
而不是普通的
CALayer
,方法是覆盖
layerClass
方法。我们还将添加一个方便的方法,将视图的层作为
CAShapeLayer
返回

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

- (CAShapeLayer *)shapeLayer {
    return (CAShapeLayer *)self.layer;
}
我们将在
layoutSubviews
中计算切片的路径,因为视图在其大小更改时会收到
layoutSubviews
消息

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

我们还调整了层的锚定点;这是层中在缩放或腐烂时不移动的点
- (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;
}
- (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;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return CGPathContainsPoint(self.shapeLayer.path, NULL, point, NO);
}

@end
@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);
}
- (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
@property (nonatomic) CGFloat minScale;
@property (nonatomic) CGFloat maxScale;
@property (nonatomic) CGFloat scale;
- (void)setScale:(CGFloat)scale {
    _scale = MAX(minScale, MIN(scale, maxScale));
    self.transform = CGAffineTransformMakeScale(_scale, _scale);
}
- (IBAction)pinched:(UIPinchGestureRecognizer *)pincher {
    if (!_selectedSlice)
        return;
    CGFloat scale = pincher.scale;
    pincher.scale = 1;
    _selectedSlice.scale = _selectedSlice.scale * scale;
}