CGRect Grid on screen? - iphone

I want to create a grid of rectangles at the centre of screen leaving some space on the edges. The need of that arises because I am spewing different sprites at random points and they keep spawning on top of eat other. So i thought if there is a way of creating a class that creates the grid and returns me with a random rect and mark it occupied as long at the sprite stays in that rect and make it free after.
If i can get some help or any tips it will be great. Any other solutions to achieve this are welcome too.
Thanks.

You could nest two for loops, one for rows and one for columns, make them both run 5 times, and in each loop increment the x position and y position by one-fifth the width and height of the screen and put these coordinates into a CGRrect. That would do what you want.

Thanks #andrewx for your help. This will create CGRect in the given range and then return a random one.
-(void) makeCGRectArray{
rectsArray = [[NSMutableArray alloc] init];
for (int x = 30; x<=420; x= x+60) {
for (int y=40; y<=280; y=y+40) {
CGRect newRect = CGRectMake(x, y, 60, 40);
[rectsArray addObject:[NSValue valueWithCGRect:newRect]];
}
}
[self getRandomCgrect:rectsArray];
}
-(CGRect) getRandomCgrect:(NSMutableArray*) rectArray{
NSInteger randomPoint = (arc4random() % (49));
CGRect randomRect = [[rectsArray objectAtIndex:randomPoint] CGRectValue];
self.isOccupied = YES;
return randomRect;
}

Related

UISegmentedControl bounds

I want to give the following aspect to an UISegmentedControl:
Note the gray background view, and the white background of the segmented control non selected item.
But, if I give a white background to my UISegmentedControl, I get the following:
Note the white square corners around the UISegmentedControl. What should I do to avoid that square corners?
Thank you in advance,
EDIT: If I change the corner radius of the UISegmentedControl's layer, as suggested by onegray, the result is better, but not perfect (note the white line at the right):
Setting the _segmentedControl.layer.cornerRadius = 5; might help.
Update: More complex clip rect to get rid of 1px right space:
CAShapeLayer* mask = [[CAShapeLayer alloc] init];
mask.frame = CGRectMake(0, 0, _segmentedControl.bounds.size.width-1, _segmentedControl.bounds.size.height);
mask.path = [[UIBezierPath bezierPathWithRoundedRect:mask.frame cornerRadius:4] CGPath];
_segmentedControl.layer.mask = mask;
Update: Matthias Bauch provided a good explanation why this whitespace appears on the right side of the UISegmentedControl. So the simplest way to remove it is making segments of fixed size and adjusting them for proper width.
If that should work for all UISegmentedControls it's a bit of a hassle.
The problem is in iOS7 the 1 pt. border between two segments does not count to the size of the segment. E.g. if the frame of your UISegmentedControl is 320 pt. wide you have to remove 1 pt. and than divide by 2.
And (320-1)/2 is 159.5. iOS floors this value down to 159 pt. And you end up with a 1 pt. border and two 159 pt. segments. Which is 319, and not 320. Hence the 1pt. line at the right of your segmentedControl.
There is a way to calculate the "actual" (the size of the rendering on screen) size of the segmentedControl. With that width you can then add a UIView with rounded corners below the UISegmentedControl.
This code should work for all configurations, even if you have manually sized segments in your segmentedControl:
- (UIView *)addBackgroundViewBelowSegmentedControl:(UISegmentedControl *)segmentedControl {
CGFloat autosizedWidth = CGRectGetWidth(segmentedControl.bounds);
autosizedWidth -= (segmentedControl.numberOfSegments - 1); // ignore the 1pt. borders between segments
NSInteger numberOfAutosizedSegmentes = 0;
NSMutableArray *segmentWidths = [NSMutableArray arrayWithCapacity:segmentedControl.numberOfSegments];
for (NSInteger i = 0; i < segmentedControl.numberOfSegments; i++) {
CGFloat width = [segmentedControl widthForSegmentAtIndex:i];
if (width == 0.0f) {
// auto sized
numberOfAutosizedSegmentes++;
[segmentWidths addObject:[NSNull null]];
}
else {
// manually sized
autosizedWidth -= width;
[segmentWidths addObject:#(width)];
}
}
CGFloat autoWidth = floorf(autosizedWidth/(float)numberOfAutosizedSegmentes);
CGFloat realWidth = (segmentedControl.numberOfSegments-1); // add all the 1pt. borders between the segments
for (NSInteger i = 0; i < [segmentWidths count]; i++) {
id width = segmentWidths[i];
if (width == [NSNull null]) {
realWidth += autoWidth;
}
else {
realWidth += [width floatValue];
}
}
CGRect whiteViewFrame = segmentedControl.frame;
whiteViewFrame.size.width = realWidth;
UIView *whiteView = [[UIView alloc] initWithFrame:whiteViewFrame];
whiteView.backgroundColor = [UIColor whiteColor];
whiteView.layer.cornerRadius = 5.0f;
[self.view insertSubview:whiteView belowSubview:segmentedControl];
return whiteView;
}
Please take care of frame changes yourself.
See this screenshot to see the difference between the two controls. All frames are 280 pt. wide.
Because of the formula UISegmentedControl uses the first controls actual size is 278 pt. And the real size of the second one is 279 pt.
The problem is that this somehow relies on the implementation of UISegmentedControl. Apple could for example change the implementation so segmentWidth that end in .5 points will be displayed. They could easily do this on retina displays.
If you use this code you should check your app on new iOS versions as early as possible. We are relying on implementation details, and those could change every day. Fortunately nothing bad happens if they change the implementation. It will just not look good.
I know this is kind of a hack but you could just use a rounded UIView with white background placed just underneath - and aligned with - the segmented control, except for the width which should be equal to the original control's width minus 1.
Result:
Just to clarify Mattias Bauch's excellent answer. You need to set the returned view as a subview to the view (which we call yourMainView) where you have your segmented control:
UIView *segmControlBackground = [self addBackgroundViewBelowSegmentedControl:yourSegmentedControl];
[yourMainView addSubview:segmControlBackground];
And you need to, of course, declare the new method in your header (.h) file:
- (UIView *)addBackgroundViewBelowSegmentedControl:(UISegmentedControl *)segmentedControl;

Why is UIView drawRect causing first pass of UIScrollview to be choppy/jerky/slow?

EDIT: It looks like the problem code is in a procedural background that I am drawing in a UIView which I am then adding as a subview to UIScrollView. The procedural code is below. It draws box shapes, which look sort of like a skyline. Any Ideas why this is slowing down the first pass of my UIScrollView? It can be as much as a thousand pixels wide or more at times. See image...
- (void)drawRect:(CGRect)rect
{
UIBezierPath *vertLine = [[UIBezierPath alloc] init];
[vertLine moveToPoint:CGPointMake(0,self.frame.size.height)];
int detail = 10;
int ranNum = 0;
int count = self.bounds.size.width/detail;
CGFloat heightIncrement = 0.0;
CGFloat minHeight = self.frame.size.height;
CGFloat xPos = 0;
CGFloat yPos = self.frame.size.height-20;
for (int i =0; i<count; i++)
{
ranNum += (arc4random() % 9)-5;
yPos -= (arc4random() % 30);
[vertLine addLineToPoint:CGPointMake(xPos,yPos)];
xPos += (arc4random() % 20)+10;
[vertLine addLineToPoint:CGPointMake(xPos,yPos)];
yPos += (arc4random() % 30);
[vertLine addLineToPoint:CGPointMake(xPos,yPos)];
xPos += (arc4random() % 30);
[vertLine addLineToPoint:CGPointMake(xPos,yPos)];
if (yPos>self.frame.size.height-10) {
yPos = self.frame.size.height-10;
}
if (yPos<self.frame.size.height-50) {
yPos = self.frame.size.height-50;
}
}
[vertLine addLineToPoint:CGPointMake(count*20,(self.frame.size.height))];
[[UIColor colorWithRed:0.0/255.0 green:38.0/255.0 blue:51.0/255 alpha:1] setFill];
[vertLine fill];
}
I have a jerky scroll view, but ONLY on the first pass. After all the views have been viewed in the scroll view, it is very smooth.
First Pass: During the first pass it appears that when each UIImageView is coming into view (from right to left) There is a jerk right when it is entering the visible area. So if you reference the attached image, you'll see UIImageView 5 entering from right to left. When this happens there is a small pause as if the scroll view is telling the image view to load/prepare to be on stage. I have tried to profile this but I don't see any problems in my code and im not sure how I can profile the methods etc. that I have not overridden. So a sub question would be... What methods are called on a subview of UIScrollView when it is entering the visible area?
As I mention I tried to do the async and other concurrent approaches, but it seems that no matter how the images are loaded, the first pass is always jerky, then its as if the UIScrollView caches the subviews. Is it possible to do this caching/loading up front...
[scrollView cacheSubViews]; I would rather have a slower startup than it to be clunky on the first scroll.
Thanks for any ideas on this or information about how the UIScrollView works with its subviews. I have seen many questions and some solutions about jerky UIScrollViews with UIImageViews as subviews. I have tried many of them, but still have a slow scrollview.
austin
imageWithContentsOfFile is a synchronous process takes a lot of tile.Use some asynchronous way to load the images and the smoothness can be achieved

How to check whether two image views are in same position?

I am working in a project where the images are drag able. In which if i put one image over the other image the image has to be changed.
What i need is
1) I want to check whether both the images are in same place or not.
2) The position of image must not be exact.It may be approximately equal
If you want to check if two UIImages intersect, this is a good way:
CGRect rect1 = myView1.frame;
CGrect rect2 = myView2.frame;
if (CGRectIsNull(CGRectIntersection(rect1, rect2))) {
//They collide
}
You have all the info you need about this here:
https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html
//for checking whether one image is inside another image
if(CGRectContainsRect(ImageView1.frame, ImageView2.frame))
{
//your code for overlapping here
}
//for checking whether one image intersects another
if(CGRectIntersectsRect(ImageView1.frame,ImageView2.frame))
{
//your code for intersection here
}
Here is an approach assuming the images are the same size
// using ints because it is easier for taking the abs value
int dx,dy;
dx = frame1.origin.x - frame2.origin.x;
dy = frame1.origin.y - frame2.origin.y;
dx = abs(dx);
dy = abs(dy);
// overlapping width and height
int ovHeight, ovWidth;
ovWidth = frame1.size.width-dx;
ovHeight = frame1.size.height-dy;
int ovArea = ovWidth*ovHeight;
int imageArea = frame1.width*frame1.height;
int percentOverlap = ovArea*100/imageArea;
if (percentOverlap > 80) {
// do work here
}
CGRect frame1 = imgView1.frame;
CGRect frame2 = imgView2.frame;
this will give you two frame structures..which consist of origin(x,y) values and size(width,height) values..
compare according to your needs.

CGRectContainsRect Not Working

I'm having problems with this method. I have two rectangles that are obviously contained within each other.(I've even graphed their coordinates manually to make sure.) When I use CGRectContainsRect to compare these two rectangles, it returns false. For the life of me, I have tried everything, scoured the net, and I can't find an answer to this problem. Anyone have any idea why? I've included the values for the CGRects when I debug to show that they are definitely within each other.
-(bool)checkBoundingBox {
bool returnItem = YES;
//Checks for sprite interaction
for (int i = 0; i < [arrGameItems count]; i++) {
CGRect rect2 = [[self getChildByTag:1] boundingBox];
CGRect rect1 = [[self getChildByTag:3] boundingBox];
// rect1 = CGRectStandardize(rect1);
// rect2 = CGRectStandardize(rect2);
if (CGRectContainsRect(rect2, rect1)) {
CCLOG(#"removed child b*&ch");
[self removeChildByTag:[arrGameItems count] cleanup:YES];
returnItem = NO;
}
}
CCLOG(#"g-dammit");
return returnItem;
}
rect1
origin x = 141 y = 76, height = 25, width = 25
rect2
origin x = 127 y = 91, height = 25, width = 25
CGRectContainsRect() checks if one rectangle completely encompasses another, not just if they intersect. From your coordinates, the rectangles don't contain each other, but just intersect. You're looking for CGRectIntersectsRect().
rect1 does not contain rect2 in your example.
Rect 1 x coordinates span from 141 to 166.
Rect 2 x coordinates span from 127 to 152.
Therefor, rect2 is not contained within rect1 (because rect2 exists within x coordinates 127-140, and rect1 does not exist in those coordinates).

iPhone SDK: repeat subviews

I have one UIView, which I'm using as my main view, and I want to repeat the subview across the screen. How exactly can I do this?
You can look at Core Animation. There is a layer called the CAReplicatorLayer that might help you. Alternatively you can use generic CALayers and set their contents all to the same image. You would just need to figure out the width of your parent view and how big you want each tile to be and then just create CALayers for each tile shifting the position of each new layer depending on your grid dimensions. Something like this:
UIImage *imageToReplicate = [UImage imageNamed:#"tile"];
for (i = 0; i < 10; ++i)
{
for (j=0; j < 10; ++j)
{
CGFloat xPos = 0.0; // Calculate your x position
CGFloat yPos = 0.0; // Calculate your y position
CALayer *layer = [CALayer layer];
[layer setBounds:CGRectMake(0.0f, 0.0f, TILE_WIDTH, TILE_HEIGHT)];
[layer setPosition:CGPointMake(xPos, yPos)];
[layer setContents:(id)[image CGImage]];
[[[self view] layer] addSublayer:layer];
}
}
You'll have to figure out the calculation for each iteration of your layer positions. Remember that by default the anchor point of the layer is its center. You either calculate it by subtracting half of the layer tile size or you can change the anchor point to be a corner instead. For more information on that, take a look at the layer geometry section of the Core Animation documentation.