1

这是我的UIPanGestureRecognizer

- (void)SwipeHandle:(UIPanGestureRecognizer*)gestureRecognizer
{
UIView *theSuperview = self.numberview;
CGPoint touchPointInSuperview = [gestureRecognizer locationInView:theSuperview];

float gapX = image1.frame.size.width / 8;
float gapY = image1.frame.size.height / 8.48;

if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
    if(!ispause && [time.text intValue] > 0){
        if(!isbegan && !isended){
            for(int i = 1; i <= 16; i++)
            {
                UIView *imageview = [self.numberview viewWithTag:i];
                if (CGRectContainsPoint(imageview.frame, touchPointInSuperview))
                {
                    isbegan = YES;
                    isreverse = NO;
                    if([[ischose objectAtIndex:i-1] boolValue] == 0)
                    {
                        currentposition = imageview.tag;
                        positionvalue += pow(i, 3);
                        currentanswer += [self converter:[NSString stringWithFormat:@"%@", [allimagenumbers substringWithRange:NSMakeRange(i-1, 1)]]];
                        [ischose replaceObjectAtIndex:i-1 withObject:[NSNumber numberWithBool:YES]];
                        [self changeimage:@"selected"];
                    }

                    previouspoint = imageview.frame.origin;
                    break;
                }
            }
        }
    }
}
else if(gestureRecognizer.state == UIGestureRecognizerStateChanged)
{
    if(isbegan && !isended)
    {
        if(touchPointInSuperview.x >= 0 && touchPointInSuperview.x <= self.numberview.frame.size.width && touchPointInSuperview.y >= 0 && touchPointInSuperview.y <= self.numberview.frame.size.height)
        {
            for(int i = 1; i <= 16; i++)
            {
                UIImageView *imageview = (UIImageView*)[self.numberview viewWithTag:i];
                if (CGRectContainsPoint(imageview.frame, touchPointInSuperview))
                {
                    if((touchPointInSuperview.x >= imageview.frame.origin.x + gapX && touchPointInSuperview.x < imageview.frame.origin.x + imageview.frame.size.width  - gapX) && (touchPointInSuperview.y >= imageview.frame.origin.y  + gapY && touchPointInSuperview.y < imageview.frame.origin.y + imageview.frame.size.height - gapY ))
                    {
                        if([[ischose objectAtIndex:i-1] boolValue] == 0 && !isreverse)
                        {
                            currentposition = imageview.tag;
                            positionvalue += pow(i, 3);
                            currentanswer += [self converter:[NSString stringWithFormat:@"%@", [allimagenumbers substringWithRange:NSMakeRange(i-1, 1)]]];
                            [ischose replaceObjectAtIndex:i-1 withObject:[NSNumber numberWithBool:YES]];
                            [self changeimage:@"selected"];

                            currentpoint = imageview.frame.origin;

                            [self.numberview drawRect:CGRectMake(self.numberview.frame.origin.x, self.numberview.frame.origin.y, self.numberview.frame.size.width, self.numberview.frame.size.height)];
                            UIGraphicsBeginImageContext(imageview.frame.size);
                            CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
                            CGContextSetStrokeColor(UIGraphicsGetCurrentContext(), red);
                            CGContextBeginPath(UIGraphicsGetCurrentContext());
                            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previouspoint.x, previouspoint.y);
                            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0f);
                            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentpoint.x,currentpoint.y);
                            CGContextClosePath(UIGraphicsGetCurrentContext());

                            previouspoint = currentpoint;


                        }
                        else
                        {
                            if(currentposition != imageview.tag)
                            {
                                isreverse = YES;
                            }
                            else
                            {
                                isreverse = NO;
                            }
                        }
                        break;
                    }
                }
            }
        }
        else
        {
            isended = YES;
            isoutofbound = YES;
            if(isbegan && isoutofbound)
                [self countinganswer];
        }
    }
}
else if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
    if(!isoutofbound)
    {
        isended = YES;
        [self countinganswer];
    }
    else
        isoutofbound = NO;
}
}

我试图画一条线,但无法画线。

问题是什么?可以给我一个示例项目吗?

4

1 回答 1

0

问题是你正在绘制一个上下文,然后什么都不做,只是泄漏。在您进行绘图的中间,您需要通过UIGraphicsGetImageFromCurrentContext(). 然后您需要将该图像分配给图像视图。

或者(更实际地)您可以存储绘制的点,并将它们绘制在视图的drawRect:函数中。

不,我没有适合您的示例项目。许多其他人(包括我自己)已经实现了这个目标,我相信你也会。如果你仍然遇到问题,你应该问一个更具体的问题,而不是把你所有的代码都放在这里,然后问“为什么这不起作用?”

一个不相关的提示:如果你不想让你的同事对你发火,不要像你正在做的那样使用一百万个嵌套的 if 语句。又丑又难读……

于 2012-11-20T06:44:37.150 回答