2012-04-09 76 views
0

當試圖一個CGPoint傳遞給點傳遞的方法,但一旦被調用的方法的CGPoint值顯示(INF,INF)內?因此,將位置CGPoint傳遞給createShapeAt方法的代碼不起作用。也許我沒有正確實現某些東西,或者我需要在傳遞時複製CGPoint?傳遞一個CGPoint另一種方法與(INF,INF)結束

- (void)handleTap:(UITapGestureRecognizer *)recognizer { 
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) 
{ 
    NSLog(@"User just tapped!"); 
    CGPoint location = [recognizer locationInView:recognizer.view]; 
    float scale = [(BasicCanvasUIView *)recognizer.view scale]; 
    location = CGPointMake(location.x/scale, location.y/scale); 
    [self createShapeAt:location]; 
} 
} 

- (void)createShapeAt:(CGPoint)point { 
//Create a managed object to store the shape 
---------------------------------------------------------------------------- 
<-- WHEN I GET HERE ON BREAKPOINT THE (point) VARIABLE SHOWS "(inf,inf)" --> 
---------------------------------------------------------------------------- 
NSManagedObject *shape = nil; 

//Randomly choose a Circle or a Polygon 
int type = arc4random() % 2; 
if (type == 0) { // Circle 
    //Create the circle 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Circle" inManagedObjectContext:self.managedObjectContext]; 
    NSManagedObject *circle = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:self.managedObjectContext]; 
    shape = circle; 

    //Randomly create a radius and set the attributes of the circle 
    float radius = 10 + (arc4random() % 90); 
    [circle setValue:[NSNumber numberWithFloat:point.x] forKey:@"x"]; 
    [circle setValue:[NSNumber numberWithFloat:point.y] forKey:@"y"]; 
    [circle setValue:[NSNumber numberWithFloat:radius] forKey:@"radius"]; 
} else { // Polygon 
    //Create the Polygon 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Polygon" inManagedObjectContext:self.managedObjectContext]; 
    NSManagedObject *polygon = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:self.managedObjectContext]; 
    shape = polygon; 

    //Get the vertices. At this point, no Vertex objects for this shape exist. 
    //Anything you add to the set, however, will be added to the Vertex entity; 
    NSMutableSet *vertices =[polygon mutableSetValueForKey:@"vertices"]; 

    //Create a random number of vertices 
    int nVertices = 3 + (arc4random() % 20); 
    float angleIncrement = (2 * M_PI)/nVertices; 
    int index = 0; 
    for (float i = 0; i < nVertices; i++) { 
     // Generate random values of each vertex 
     float a = i * angleIncrement; 
     float radius = 10 + (arc4random() % 90); 
     float x = point.x + (radius * cos(a)); 
     float y = point.y + (radius * sin(a)); 

     // Create the Vertex managed object 
     NSEntityDescription *vertexEntity = [NSEntityDescription entityForName:@"Vertex" inManagedObjectContext:self.managedObjectContext]; 
     NSManagedObject *vertex = [NSEntityDescription insertNewObjectForEntityForName:[vertexEntity name] inManagedObjectContext:self.managedObjectContext]; 

     //Set teh values for the vertex 
     [vertex setValue:[NSNumber numberWithFloat:x] forKey:@"x"]; 
     [vertex setValue:[NSNumber numberWithFloat:y] forKey:@"y"]; 
     [vertex setValue:[NSNumber numberWithFloat:index++] forKey:@"index"]; 

     //Add the Vertex object to the relationship 
     [vertices addObject:vertex]; 

    }// ----------------- END IF/ELSE (circle and polygon done) ------------------------------------------------------------------------------------- 

    //Set the shapes color 
    [shape setValue:[self makeRandomColor] forKey:@"color"]; 

    //Add the same shape to both canvases 
    [[topView.canvas mutableSetValueForKey:@"shapes"] addObject:shape]; 
    [[bottomView.canvas mutableSetValueForKey:@"shapes"] addObject:shape]; 

    //Save the context 
    NSError *error = nil; 
    if (![self.managedObjectContext save:&error]) { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 

    // Tell the views to repaint themselves 
    [topView setNeedsDisplay]; 
    [bottomView setNeedsDisplay]; 

} 
} 
+0

有才能通過檢查你的價值? – lnafziger 2012-04-09 03:22:48

回答

5

一個明顯的可能性是recognizer.view.scale爲零。你測試過了嗎?

將這個聲明handleTap:,你叫createShapeAt:前:

NSLog(@"recognizer.view.scale = %f", scale); 

那是什麼語句的輸出?如果它爲零,那就是你的問題。您需要使用不爲零的比例。

+0

是的,我在taphandler的self.createShapeAt中斷,CGPoint在那裏。當我調用createShapesAt時會中斷,但是當我到達shape = nil line(inf,inf)時。什麼是inf? – jdog 2012-04-09 03:55:57

+0

'inf'表示無窮大。當你用'float'或'double'除以零時通常會得到一個特殊的值。因此,我的建議是「規模」爲零。 – 2012-04-09 03:57:38

+0

我已經更新了我的答案。 – 2012-04-09 04:00:04

相關問題