Here is my 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;
}
}
I tried to draw a line but the line cannot be draw.
What is the problem? and can give me a sample project?
The problem is you are drawing into a context, and then doing nothing with it and simply leaking. In the middle where you do your drawing, you need to get the image out of that context via
UIGraphicsGetImageFromCurrentContext(). Then you need to assign that image to an image view.Alternatively (more realistically) you can store the points that are drawn, and draw them in your view’s
drawRect:function.No, I don’t have a sample project for you. Many other people have accomplished this goal (including myself) and I have faith that you will as well. If you are still having trouble you should ask a more specific question instead of just dumping all your code here and asking “Why doesn’t this work?”
An unrelated tip: If you don’t want your coworkers to rage at you, don’t use a million nested if statements like you are doing. It’s ugly and hard to read…