Lindsey
Lindsey

Reputation: 1

Need help with CGRect & CGPoint for ios dev

Hey, I'm trying to implement double tap zooming and I'm trying to work with code from Apple's ScrollViewSuite example and several of the lines are giving me errors.

The first one is

tapLocation = midpointBetweenPoints(tapLocation, [touch locationInView:touch.view]);

And it says incompatible types in assignment. I haven't been able to find much information about midpointBetweenPoints other than that it compares two CGPoints and I believe that's what I'm passing it.

Second section that gives an error is

    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];

And it gives me invalid initializer.

Does anyone know what I'm doing wrong?

Upvotes: 0

Views: 1237

Answers (1)

Tommy
Tommy

Reputation: 100622

Both midpointBetweenPoints and zoomRectForScale:withCenter: are part of the example code rather than part of iOS itself.

midpointBetweenPoints returns a CGPoint (relevant to the error about assignment), but is not declared in a header anywhere. You'll see that in the sample in which it is used, 3_Tiling, it is defined at line 176 of TapDetectingView.m, declared at line 54 and used at 118 and 139. My guess would be that you're either not including the code in your project at all, or are including the definition but omitting the declaration. Objective-C is a superset of C, so follows the C rules. In C, any function for which the declaration cannot be found is assumed to return 'int'. Quite probably you need to add the declaration:

CGPoint midpointBetweenPoints(CGPoint a, CGPoint b);

Somewhere before your use.

At a guess, your use of zoomRectForScale:withCenter: is likely to be a similar issue. If declarations aren't found for Objective-C methods then they're assumed to return 'id', which is a pointer to a generic Objective-C object. CGRect is a C struct, so casting a pointer to it makes no sense. Assuming you've included the code from lines 401 to 416 of RootViewController.m, you also need to ensure the declaration is visible to the calling code. In the sample code, this is achieved on line 80 with the declaration:

@interface RootViewController (UtilityMethods)
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end

Declaring additions like that inside a source file is a way of approximating private methods in Objective-C. If you want it to be public, you can just add it to the interface declaration proper.

Upvotes: 1

Related Questions