I'm just overriding a MKOverlay so I'm looking for good practice in Apple developers sample code ...
When I see this :
// bite off up to 1/4 of the world to draw into.
MKMapPoint origin = points[0];
origin.x -= MKMapSizeWorld.width / 8.0;
origin.y -= MKMapSizeWorld.height / 8.0;
MKMapSize size = MKMapSizeWorld;
size.width /= 4.0;
size.height /= 4.0;
boundingMapRect = (MKMapRect) { origin, size };
MKMapRect worldRect = MKMapRectMake(0, 0, MKMapSizeWorld.width, MKMapSizeWorld.height);
boundingMapRect = MKMapRectIntersection(boundingMapRect, worldRect);
Many questions
Why Apple do the intersection with worldRect after calculate the good boundingMapRect ?
By Security ?
boundingMapRect inter worldRect isn't always boundingMapRect ?
If I have two MKMapPoint represent my CGRect, I haven't to do it, right ?
Regards,
EDIT
so I have just to do like this ?
// set the smallest rectangle which able to contain this overlay
MKMapPoint leftTopPoint = MKMapPointForCoordinate(leftTopCorner);
MKMapPoint rightBottomPoint = MKMapPointForCoordinate([self bottomRightCoord]);
boundingMapRect = MKMapRectMake(leftTopPoint.x, leftTopPoint.y,
fabs(leftTopPoint.x - rightBottomPoint.x),
fabs(leftTopPoint.y - rightBottomPoint.y));
That code is from the Breadcrumb sample which is perhaps a little unusual compared to most overlays in that it's dynamically updated. They are actually setting the bounding area to be a quarter of the world area which is a bit unusual in itself, normally you would expect it to be much smaller. I think the intersection is just to make sure that the resulting rect is definitely inside the full world rect. I'm not sure I'd recommend this code as best practice unless you are doing something similar (i.e. creating a dynamic overlay). Presumably even this code could be caught out if you were to run the Breadcrumb app while flying around the world non-stop, or maybe even just crossing the international dateline...
Related
I'm checking the breadcrumb example:
http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html
On the iphone simulator, if I simulate somewhere off the initial US view from the MKMapView, drawMapRect:zoomScale:inContext: doesn't get called when it should draw something.
Why is that? When is the method called? What can I do to make it work?
This is because of limitation set to display routes in the region of quarter of world size in height and width around the current location.
Check this code out in CrumbPath.m
// bite off up to 1/4 of the world to draw into.
MKMapPoint origin = points[0];
origin.x -= MKMapSizeWorld.width / 8.0;
origin.y -= MKMapSizeWorld.height / 8.0;
MKMapSize size = MKMapSizeWorld;
size.width /= 4.0;
size.height /= 4.0;
boundingMapRect = (MKMapRect) { origin, size };
MKMapRect worldRect = MKMapRectMake(0, 0, MKMapSizeWorld.width, MKMapSizeWorld.height);
boundingMapRect = MKMapRectIntersection(boundingMapRect, worldRect);
What version of xCode are you using?
These are the requirements for the app:
BUILD REQUIREMENTS:
iOS 5.0 SDK or later
RUNTIME REQUIREMENTS:
iOS 4.0 or later
I have a custom UIView that contains an interactive drawing that is drawn in the drawRect function of the view. On the iPad the drawing size is 1024 x 768. For the iPhone I shrink the drawing for the iPhone using CGContextScaleCTM. To keep the proper aspect ratio on my drawings I shrink the view to 480 x 360 and set the y value of the view to -20 (effectively cropping 20 pixels off the top and bottom of the View, which is fine). Everything looks correct now, but I need to convert the touch coordinates from iPhone to iPad coordinates for the interactive portions of my view to work. If I make the uiview 320 high and use
point.y *=768/320
for converting the y value the locations are correct (but my drawing is distorted) I've done some tests hard coding point in so I know this should work but I'm having a hard time getting the math to work with the crop. Here is what I have so far:
CGPoint point = [[touches anyObject] locationInView:self.view];
[self endTouch:&point];
NSLog(#"true point: %#",NSStringFromCGPoint(point));
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
point.x *= 1024/480;
point.y *= 768/360;
NSLog(#"corrected point: %#",NSStringFromCGPoint(point));
}
Can anybody help me with doing this conversion? Thanks!
1024/480 and 768/360 perform integer division. The result of both operations is 2, but you really want 2.133333. Replace the relevant lines in your code with the following:
point.x *= 1024.0/480.0;
point.y *= 768.0/360.0;
This will return the value you're looking for and scale your point's x and y values accordingly.
You might be better served (in terms of readability) by replacing these literals with a #define macro. At the top of your code, put the following:
#define SCALING_FACTOR_X (1024.0/480.0)
#define SCALING_FACTOR_Y (768.0/360.0)
and then modify your code to use that:
point.x *= SCALING_FACTOR_X;
point.y *= SCALING_FACTOR_Y;
That way, it will be more clear as to what you're actually doing.
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Score : #" fontName:#"Arial" fontSize:14];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width + 0, size.height + 0 );
// add the label as a child to this Layer
[self addChild: label];
label.position = ccp( size.width + 0, size.height + 0 );
how would i get that label in the bottom left corner. I do not understand the coordinates system very well. From what I understand Y is bottom. X is far left. so how come when I use that code, the position of the label is top right. My application is portrait.
Also would it better to have the label as a sprite, or just keep it like that. ( its to hold a score )
In codos2d, the point (0,0) is the bottom left corner of the screen. Think of it as quadrant 1 on a x,y plane where x increases as you go right, and y increases as you go up.
So to get the label in the bottom left just do label.position = ccp(0, 0); but this might be the default so you might not have to do anything.
consider first answer for your first question, and for second question answer is, if label is for score, then it's better you use label, rather than sprites(because you'll need 1000s of them and it'll be heavy as well as complicated)
CCLabelTTF is expensive if updated rapidly (many times per second), but is fine if you're not updating it. One preferred way is to use CCLabelBMFont (I think that's the class...). You'll need the .fnt file and .png to go with it, but you can make those with a tool like Glyph Designer from 71squared.
I have a UIView that I want to rotate when I move my finger across the screen (like in a circle). I want the UIView to rotate so that it faces my touchpoint. I then want to shoot something from my UIView (like a bullet) in the direction that the UIView is facing. Any Ideas???
Glad I remember triginometry
-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{
float dX = touchPoint.x-objPos.x; // distance along X
float dY = touchPoint.y-objPos.y; // distance along Y
float radians = atan2(dY, dX); // tan = opp / adj
//Now we have to convert radians to degrees:
float degrees = radians*M_PI/360;
return degrees;
}
Once you have your nice method, just do this:
CGAffineTransform current = view.transform;
[view setTransform:CGAffineTransformRotate(current, [view degreesTorotateObjectWithPosition:view.frame.origin andTouchPoint:[touch locationInView:parentView]]
//Note: parentView = The view that your object to rotate is sitting in.
This is pretty much all the code that you'll need.The math is right, but I'm not sure about the setTransform stuff. I'm at school writing this in a browser. You should be able to figure it out from here.
Good luck,
Aurum Aquila
Right now I'm populating a UIScrollView with a series of views. The views need to be warped to make the UIScrollView appear like a carousel. In other words when the user scrolls it needs to be like a circle. I've never done anything quite like this before, but I'm assuming CoreAnimation is out of the question and OpenGL needs to be used. If this is possible with CoreAnimation or Quartz then I really just need a sample on how to warp the views and I can figure the rest out myself but I'm not familiar with OpenGL.
If you want to warp the views, you'll either need OpenGL or you could use Core Animation's CAShapLayer which allows you to specify a bezier path which can have this curve in it. But keep in mind that this curving you're seeing is likely just an optical illusion (though in your image above it looks like an actual curve). If you get enough rectangles with the correct y axis rotation in a row, I think you can come up with the effect you're looking for with straight Core Animation. I'm pretty sure that's how things are implemented in the Core Animation demos Apple provided a couple years ago. Here's a screenshot from the video from that presentation:
I messed around with the transform of a view's layer a little bit and came up with this:
- (IBAction)sliderDidChange:(id)sender
{
CGFloat value = [(UISlider*)sender value];
CGFloat xOff = value - 0.5;
CATransform3D trans = CATransform3DIdentity;
trans.m34 = 1.0f / -1000.0f;
trans = CATransform3DRotate(trans, degreesToRadians(xOff * -25.0f), 0.0f, 1.0f, 0.0f);
trans = CATransform3DTranslate(trans, 0.0f, 0.0f, 900.0f * fabs(xOff));
[[frameView layer] setTransform:trans];
CGPoint center= [frameView center];
[frameView setCenter:CGPointMake(1024.0 * value, center.y)];
}
I threw together a demo project that shows how the rotation works in response to a slider. It doesn't use a scroll view so you would have to adapt it, but I think you can track the current scroll offset and apply the transform accordingly. Not sure if it will help but there it is.
In my experience, it is a bit tricky to get the values right. The way to give a view perspective is by manipulating it's layer transform. I have used the following method to achieve the transfor for a similar effect:
-(CATransform3D)makeTransformForAngle:(CGFloat)angle from:(CATransform3D)start{
CATransform3D transform = start;
// the following two lines are the key to achieve the perspective effect
CATransform3D persp = CATransform3DIdentity;
persp.m34 = 1.0 / -1000;
transform = CATransform3DConcat(transform, persp);
transform = CATransform3DRotate(transform,angle, 0.0, 1.0, 0.0);
return transform;
}
This was done to create a "flip page" animation, so you may need to adapt. To use it, do the following:
flip_page.layer.transform = [self makeTransformForAngle:angle from:CATransform3DIdentity];
where flip_page is a UIView. Cheers!