如何使用touchesMoved移动UIBezierPath对象?

时间:2013-02-06 14:39:03

标签: iphone uibezierpath touchesmoved quartz-core

从bezier路径对象绘制直线和圆后,我想现在在屏幕上移动这些对象。首先,当我触摸路径对象时,应该选择使用containsPoint:方法完成的操作。

现在我希望这个选中的对象应该随着我拖动我的fingure而移动。我想知道如何将描边的bezierpath对象移动到新位置?

这是我的触摸开始的代码:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self];
    //NSLog(@"start point:- %f, %f", startPoint.x, startPoint.y);

    isHitInPath = NO;
    if(!isTextMode)
    {

        for (NSDictionary *testDict in pathArray)
        {
            if([((UIBezierPath *)[testDict objectForKey:@"path"]) containsPoint:startPoint])// if starting touch is on an object of bezierpath
            {
                NSLog(@"touch point is in path: %@ >>>>>>>>>>>>>>", [testDict objectForKey:@"path"]);
                isHitInPath = YES;

                currentSelectedPath = ((UIBezierPath *)[testDict objectForKey:@"path"]);

                CAShapeLayer *centerline = [CAShapeLayer layer];
                centerline.path = currentSelectedPath.CGPath;
                centerline.strokeColor = [UIColor whiteColor].CGColor;
                centerline.fillColor = [UIColor clearColor].CGColor;
                centerline.lineWidth = 1.0;
                centerline.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:6], [NSNumber numberWithInt:6], nil];
                [self.layer addSublayer:centerline];

                // showing animation on line
                CABasicAnimation *dashAnimation;
                dashAnimation = [CABasicAnimation animationWithKeyPath:@"lineDashPhase"];

                [dashAnimation setFromValue:[NSNumber numberWithFloat:0.0f]];
                [dashAnimation setToValue:[NSNumber numberWithFloat:45.0f]];
                [dashAnimation setDuration:1.0f];
                [dashAnimation setRepeatCount:10000];

                [centerline addAnimation:dashAnimation forKey:@"linePhase"];


                break;
            }
        }
}

移动的正确方法应该是什么(或者可能是删除旧路径对象然后创建相同大小和数字的新路径然后移动它)移动的路径对象。

1 个答案:

答案 0 :(得分:2)

很抱歉迟到的回复,我解决了。这是我的应用程序中的代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self];

    if(isAnyShapeSelected) // shape is selected and getting moved
    {
                [self deselectShape];// deselect the selected shape

                // then create a new bezier path object with a new location (transformed bezierpath object)
                CGPathRef _path = currentSelectedPath.CGPath;// currentSelectedPath is a path which we have selected in touches begin (or while moving)
                CGAffineTransform transform = CGAffineTransformIdentity;
                transform = CGAffineTransformTranslate(transform, endPoint.x-startPoint.x, endPoint.y-startPoint.y);

                _path =  (CGPathCreateMutableCopyByTransformingPath(_path, &transform));

                currentSelectedPath.CGPath = _path;

//                UIBezierPath *uiPath = [UIBezierPath bezierPathWithCGPath:_path];
//                uiPath.lineWidth = currentSelectedPath.lineWidth;

                startPoint = endPoint;

                [self selectShape:currentSelectedPath];// select the shape again

                [dict_path removeAllObjects];//remove all object from current drawing dictionary

                //            NSLog(@"pathArray: %@", pathArray);

                // Add values to dictionary
                if([[pathArray objectAtIndex:selectedShapeIndex] objectForKey:@"shape"])// if key 'shape' exist for the selected shape, if filled arrow is selected
                    [dict_path setValue:@"arrow" forKey:@"shape"]; // then there must be no affect on it while moving it
                [dict_path setValue:currentSelectedPath forKey:@"path"];
                [dict_path setValue:[[pathArray objectAtIndex:selectedShapeIndex] objectForKey:@"color"] forKey:@"color"];

                [pathArray removeObjectAtIndex:selectedShapeIndex];// REMOVING object frm pathArray
                NSDictionary *tempDict = [NSDictionary dictionaryWithDictionary:dict_path];
                [pathArray insertObject:tempDict atIndex:selectedShapeIndex];// addding selected shape at every pixel movement
                [dict_path removeAllObjects];

                CGPathRelease(_path);
                [self setNeedsDisplay];
    }
}


// here is my draw rect method
- (void)drawRect:(CGRect)rect
{

    for(NSDictionary *_pathDict in pathArray)
    {
        if([_pathDict objectForKey:@"shape"])// if shape is arrow then fill it(shape key is taken for arrow)
        {
            [((UIColor *)[_pathDict valueForKey:@"color"]) setFill]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
            [[_pathDict valueForKey:@"path"] fill];
        }
        [((UIColor *)[_pathDict valueForKey:@"color"]) setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        [[_pathDict valueForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }

    if([dict_path objectForKey:@"shape"])
    {
        [[dict_path objectForKey:@"color"] setFill]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        [[dict_path objectForKey:@"path"] fill];
    }

    [[dict_path objectForKey:@"color"] setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
    [[dict_path objectForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

}

-(void)deselectShape // deselect shape
{
    [centerline removeFromSuperlayer];
    isAnyShapeSelected = NO;
    [btnResize removeFromSuperview];

    // show hide delete button
    [self.delegate showDeleteButton:isAnyShapeSelected];//delegate to active and inactive delete button
}

如何在touchesBegin上选择一个形状,请在How do I detect a touch on a UIBezierPath and move a ball along that?

上查看我的答案