Cocos2D touches with CGRects? - ios

I am trying to essentially split the screen into four different parts with four different rects like so:
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.width;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.height;
CGRect jumpleftRect = CGRectMake(0, screenHeight/2, screenWidth/2, screenHeight/2); //1
CGRect runleftRect = CGRectMake(0, 0, screenWidth/2, screenHeight/2); //2
CGRect jumprightRect = CGRectMake(screenWidth/2, screenHeight/2, screenWidth/2, screenHeight/2); //3
CGRect runrightRect = CGRectMake(screenWidth/2, 0, screenWidth/2, screenHeight/2); //4
And then I am cycling through a touchArray and checking if the touch location was within, lets say for example for this question, the first rect:
NSArray *touchArray = [NSMutableArray arrayWithArray:touchArray];
for (UITouch *touchInArray in touchArray) {
CGPoint toucharrayPosition = [touchInArray locationInNode:self];
if (CGRectContainsPoint(jumpleftRect, toucharrayPosition)) { //1st rect }
However it seems like the rect is in the completely wrong spot. This never gets called even though my finger is clearly on the top left part of the screen. My anchorPoints are untouched and normal so I am not sure why this isn't working. Is there some specific reason why the touches aren't registering as in the rects even though in retrospect they should be?

Here are somethings to look into -
Are the values you getting from UIScreen correct? Is there a reason you are using UIScreen and not CCDirector's viewSize property (if using v3, winSize if using v2)?
Did you enable touches and is this code that you have inside a touch began or ended method? If so then when you debugged, did you get the correct screen position from locationInNode? Also during debugging are the rects still correct?

Not sure if this was new behavior in iOS 8 but I just reversed my screenWidth and screenHeight to this:
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
Now things are working very smoothly and nicely.

Related

Adding a button to last page of UIScrollView

I have a UIScrollView in my application and i want to basically add a button on the final page. Right now i add the button in story board under the Scroll View and set the X parameter of the button to 1050 which is (screenWidth*3 + 90) with screenWidth as 320 (iPhone5). So as you can see it works on iPhone5 but doesn't work on larger screens 6/6Plus. I tried to readjust the button programatically but it didn't work.
-(void)viewDidAppear:(BOOL)animated
{
...
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
[self.tutorialScrollView addSubview:page1];
[self.tutorialScrollView addSubview:page2];
[self.tutorialScrollView addSubview:page3];
[self.tutorialScrollView addSubview:page4];
[self.tutorialScrollView setContentSize:CGSizeMake(screenWidth*4, screenHeight)];
//readjust button
CGRect btFrame = self.tutorialFinish.frame; //self.tutorialFinish is a UIButton
btFrame.origin.x = (90 + screenWidth * 3);
btFrame.origin.y = 492/568 * screenHeight;
self.tutorialFinish.frame = btFrame;
}
Try to adjust center of the btn:
self.tutorialFinish.center = CGPointMake(90 + screenWidth * 3 + self.tutorialFinish.frame.size.width/2,
492/568 * screenHeight + self.tutorialFinish.frame.size.height/2);;
You should wrap your UIScrollView with some other UIView to know the screen size. You can later pass it in automatic way to your scroll view as lets say layoutView. In code I mean, I don't know how to do such a thing in Storyboards but I assume it is possible.
From then you should be able to adjust your views to any existing or future screen size.
CGRect buttonFrame = CGRectMake(screenwidth * (page-1)+ (screenwidth
-self.button.frame.size.width)/2, y position, button width, button height);

How do I create a home Scroll view like Instagram?

I'm trying to create a home scroll, inside it, is a UIView that contains 2 UIImage, 2 UILabel and 2 UIButton. I was trying to copy the entire view and paste it above the copied UIView and so on.
I was trying to do it with this:
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: self.contentView];
UIView *newView = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
[self.homeScroll addSubview:newView];
It apparently copy paste it correctly but my problem is that I want the newView duplicate stay one above each other. I was trying to position them like:
newView.center = CGPointMake(0.0, screenRect.size.height * i);
and
frame.origin.x = 0; // new x coordinate
frame.origin.y = screenRect.size.height * i; // new y coordinate
newView.frame = frame;
and
newView.layer.frame = CGRectMake(0, screenRect.size.height * i, self.homeScroll.frame.size.width, self.homeScroll.frame.size.height);
But without success.In all cases i is the typical int i variable of a for loop that run a requested object from a web service. And screenRect.size.height is the actual device screen height obtained from here CGRect screenRect = [[UIScreen mainScreen] bounds];. I increased the height of the UIScroll view like this:
CGSize newFrame = (CGSize){0 , screenRect.size.height * numberOfItems};
[self.homeScroll setContentSize:newFrame];
And works as expected.
I 've read About Collection View but it does not seems to be the solution.
Here is a Screen Shot.

IOS Element not moving when frame updated the first time but moves on the second update

I have an object of type UINavigationBar that I'm trying to reposition when a UITextField object triggers it's Editing Did Begin event. However I'm running into a strange error where when the following code is called:
- (IBAction)prepForTabBarView:(id)sender{
NSLog(#"Test");
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat navBarPosition = screenHeight - 350 - 64;
[self.navBar setFrame:CGRectMake(0, navBarPosition, screenWidth , 44)];
}
The first time the event is called the navBar doesn't move however the second time it's called the navBar moves. However both times the NSLog appears. What's going on here? And how can I fix it?

How to I resize an ImageView in the ViewDidLoad?

I have an imageView that is resized in two different parts of my code. This works great in my textFieldDoneEditing. However, when I use the same code in my viewDidLoad, I get a different sized view. Is there a difference with doing this in the that method?
Here's the code:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGRect frame = CGRectMake(0,0, screenWidth, screenWidth);
PuzzleImage.frame = frame;
Thank you
If your ViewController is landscape mode, viewDidLoad always gets main screen's bounds in portrait mode.
So, solution is setFrame in viewWillAppear to get right bounds.

How to drawrect paintcode content centre horizontal on landscape iOS on retina iPad?

I'm getting in a bit of a muddle trying to draw some paintcode graphics code in the middle of the screen horizontally.
I'm really not sure if scale or landscape orientation affects this.
I've created a UIView subclass and addSubview in viewDidLoad like so...
MenuTitleView *fancyView = [[MenuTitleView alloc] initWithFrame:self.view.bounds];
[[self view] addSubview:fancyView];
Rather than the size PaintCode gave me...
CGRect textRect = CGRectMake(0, 0, 418, 129);
I'm trying to determine the screen / view width and the size of the canvas width.
I've tried various things without success.
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat w = screenRect.size.width;
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = (w - width) * 0.5f;
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
Scale is not an issue, because Quartz and UIKit now work in 'points' rather than in pixels, so that's already accounted for. However orientation is not, [UIScreen mainScreen] will return the same rect either for applicationFrame or bounds etc, it's oblivious to orientation.
I like to put a solution in a category on UIView or UIViewController, because you'll reuse it plenty. something like this...
-(CGRect )workingCanvas{
CGRect screenRect = [UIScreen mainScreen].bounds;
BOOL landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
CGRect result = CGRectZero;
CGFloat lesserDimension = (screenRect.size.width < screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
CGFloat greaterDimension = (screenRect.size.width > screenRect.size.height) ? screenRect.size.width : screenRect.size.height;
result.size.width = (landscape) ? greaterDimension : lesserDimension;
result.size.height = (landscape) ? lesserDimension : greaterDimension;
if ([[UIApplication sharedApplication]isStatusBarVisible]){
result.size.height -= [[UIApplication sharedApplication]statusBarFrame].size.height;
}
return result;
}
if you want to position your view just in the middle of the screen try this
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGFloat width = 500;
CGFloat height = 200;
CGFloat x = CGRectGetMidX(screenRect);
CGFloat y = 20;
CGRect textRect = CGRectMake(x, y, width, height);
already tested is working
Good Luck!!

Resources