How can I read the coordinates of a button? - objective-c

UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background.jpg"]];
self.view = contentView;
[contentView release];
[contentView addSubview:scrollView];
scrollView.contentSize = CGSizeMake(1800,480);
UIScrollView* tscrollView = [[UIScrollView alloc] initWithFrame:(CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = 320.0f, .size.height = 5480.0f}];
tscrollView.contentSize = CGSizeMake(1800,192);
Object1 = [UIButton buttonWithType:1];
Object1.frame = CGRectMake(383, 250, 256, 192);
UIImage *buttonImage = [UIImage imageNamed:#"02_1024_768.jpg"];
[Object1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
//[self.view addSubview:Object1];
[scrollView addSubview:Object1];
Object5 = [UIButton buttonWithType:1];
Object5.frame = CGRectMake(1487, 250, 256, 192);
UIImage *buttonImage1 = [UIImage imageNamed:#"p5.jpg"];
[Object5 setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[scrollView addSubview:Object5];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 100, 200, 200)];
[myLabel setBackgroundColor:[UIColor clearColor]];
myLabel.text = #"Button1 is in range";
[myLabel setTextAlignment:UITextAlignmentCenter];
[myLabel setTextColor:[UIColor whiteColor]];
NSLog(#"x = %d",Object1.center.x);
position = CGPointMake(0,0);
Object1.center = CGPointMake(Object1.center.x+position.x,Object1.center.y+position.y);
if((Object1.center.x >341) && (Object1.center.x < 597)){
[myLabel setHidden:NO];
}
else {
[myLabel setHidden:YES];
}
[self.view addSubview:myLabel];
Here is the code, but seems that the x coordinates always equals to 0. Could some1 tell me why and how to fix it? Thanks in advance.

Try this
NSLog(#"x=%f",Object1.center.x);
coordinates are float value and you print like int so you get 0. i check your code in project

Related

objective-c ios auto layout NSLayoutConstraint

UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, frameWidth, frameHeight)];
headerView.backgroundColor = UIColorFromRGB(0x5F70B9);
UIImage* leftImage = [UIImage imageNamed: #"search_list"];
UIImageView* leftImageView = [[UIImageView alloc] initWithImage: leftImage];
[leftImageView setFrame: CGRectMake(10, 15, 70, 50)]; // CGRectMake(10, 15, 70, 50)
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(80, 20, 160, 40)];
textView.backgroundColor = UIColorFromRGB(0x5F70B9);
textView.textColor = UIColorFromRGB(0xFFFFFF);
[textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3]];
textView.text = STLocalizedString(#"message_overall_non_reply");
textView.editable = NO;
UIImage* rightImage = [UIImage imageNamed: #"arrow_mask"];
UIImageView* rightImageView = [[UIImageView alloc] initWithImage: rightImage];
[rightImageView setFrame:CGRectMake(380, 30, 15, 20)];
[headerView addSubview: leftImageView];
[headerView addSubview: textView];
[headerView addSubview: rightImageView];
Expect image:
enter image description here
Now i am hardcoding the origin x, y and everything. I want to use auto layout so that the left image and right image act as leading and trailing icon.
any suggestion?
Tried something like below, but not working:
`
[leftImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *leftImageViewConstraint = [NSLayoutConstraint constraintWithItem: leftImage attribute: NSLayoutAttributeLeading relatedBy: NSLayoutRelationEqual toItem: headerView attribute: NSLayoutAttributeLeading multiplier: 1 constant: 0];
[leftImageView addConstraints: #[leftImageViewConstraint]];
`
You need to activate each constraint that you add. When you have a bunch of constraints to set it's a lot easier to use NSLayoutConstraint activateConstraints. And using the various "anchor" properties for setting up the constraints is simpler than using the long form of creating an NSLayoutConstraint.
Here's your code updated with lots of constraints:
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, frameWidth, frameHeight)];
headerView.backgroundColor = UIColorFromRGB(0x5F70B9);
UIImage* leftImage = [UIImage imageNamed: #"search_list"];
UIImageView* leftImageView = [[UIImageView alloc] initWithImage: leftImage];
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.backgroundColor = UIColorFromRGB(0x5F70B9);
textView.textColor = UIColorFromRGB(0xFFFFFF);
textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle3];
textView.text = STLocalizedString(#"message_overall_non_reply");
textView.editable = NO;
UIImage* rightImage = [UIImage imageNamed: #"arrow_mask"];
UIImageView* rightImageView = [[UIImageView alloc] initWithImage: rightImage];
leftImageView.translatesAutoresizingMaskIntoConstraints = NO;
textView.translatesAutoresizingMaskIntoConstraints = NO;
rightImageView.translatesAutoresizingMaskIntoConstraints = NO;
[headerView addSubview: leftImageView];
[headerView addSubview: textView];
[headerView addSubview: rightImageView];
[NSLayoutConstraint activateConstraints:#[
[leftImageView.leadingAnchor constraintEqualToAnchor:headerView.leadingAnchor constant:10],
//[leftImageView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:15],
[leftImageView.centerYAnchor constraintEqualToAnchor:headerView.centerYAnchor],
[leftImageView.widthAnchor constraintEqualToConstant:70],
[leftImageView.heightAnchor constraintEqualToConstant:50],
[rightImageView.trailingAnchor constraintEqualToAnchor:headerView.trailingAnchor constant:-10],
//[rightImageView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:30],
[rightImageView.centerYAnchor constraintEqualToAnchor:headerView.centerYAnchor],
[rightImageView.widthAnchor constraintEqualToConstant:15],
[rightImageView.heightAnchor constraintEqualToConstant:20],
[textView.leadingAnchor constraintEqualToAnchor:leftImageView.trailingAnchor constant:10],
[textView.trailingAnchor constraintEqualToAnchor:rightImageView.leadingAnchor constant:-10],
[textView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:20],
[textView.bottomAnchor constraintEqualToAnchor:headerView.bottomAnchor constant:-20],
]];
I made a few guesses here. Obviously you can change these to suit your needs.

adding UIlabel to camera overlay view uiimagepickercontroller Xcode objc

I want to add some UILabels on top of my UIImagePickerController's custom OverlayView but can't seem to get it working.
Here is my code.
-(IBAction)getPhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
OverlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,1024)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(OverlayView.frame.size.height/2, OverlayView.frame.size.width/2, 30, 100)];
label.textColor = [UIColor whiteColor];
label.text = #"find Me";
[self.OverlayView addSubview:label];
[self.OverlayView bringSubviewToFront:label];
picker.cameraOverlayView = OverlayView;
CGSize screenSize = [[UIScreen mainScreen] bounds].size; // 320 x 568
float scale = screenSize.height / screenSize.width*3/4; // screen height divided by the pickerController height ... or: 568 / ( 320*4/3 )
CGAffineTransform translate=CGAffineTransformMakeTranslation(0,(screenSize.height - screenSize.width*4/3)*0.5);
CGAffineTransform fullScreen=CGAffineTransformMakeScale(scale, scale);
picker.cameraViewTransform =CGAffineTransformConcat(fullScreen, translate);
[self presentViewController: picker animated:YES completion:NULL];
}
the overview shows up and it is full screen but no label
any ideas
Your label is just offscreen, check its frame
You can place it for instance like that, at least it will be visible
self.overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.overlayView.frame.size.width/2, self.overlayView.frame.size.height/2, 30, 100)];

Move particular image

I have images under scrollview. Awesomeview having 3 images. When i click button the awesome view go to center. How to move particular image from center of the view. When i use UIPanGestureRecognizer, 3 images are moving. I can't move particular image
code:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 500, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
// CGFloat xOrigin = i * self.view.frame.size.width;
// awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
awesomeView.backgroundColor=[UIColor clearColor];
[scroll addSubview:awesomeView];
imagView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100,100)];
[imagView setImage:[UIImage imageNamed:#"img.png"]];
[awesomeView addSubview:imagView];
[imagView setTag:0];
[imagView setUserInteractionEnabled:YES];
imagView1 = [[UIImageView alloc] initWithFrame:CGRectMake(110, 0, 100,100)];
[imagView1 setImage:[UIImage imageNamed:#"img1.png"]];
[awesomeView addSubview:imagView1];
[imagView1 setTag:1];
imagView2 = [[UIImageView alloc] initWithFrame:CGRectMake(210, 0, 100,100)];
[imagView2 setImage:[UIImage imageNamed:#"img10.png"]];
[awesomeView addSubview:imagView2];
[imagView2 setTag:2];
vc=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scroll addSubview:vc];
UIButton *men = [UIButton buttonWithType:UIButtonTypeCustom];
[men setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateNormal];
// [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];
[men setFrame:CGRectMake(10,0,100,100)];
// [arrowButton addTarget:self action:#selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[vc addSubview:men];
UIButton *departments = [UIButton buttonWithType:UIButtonTypeCustom];
[departments setImage:[UIImage imageNamed:#"img3.png"] forState:UIControlStateNormal];
// [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];
[departments setFrame:CGRectMake(120,0,100,100)];
// [arrowButton addTarget:self action:#selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[vc addSubview:departments];
UIButton *blog = [UIButton buttonWithType:UIButtonTypeCustom];
[blog setImage:[UIImage imageNamed:#"sofa3.png"] forState:UIControlStateNormal];
// [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];
[blog setFrame:CGRectMake(230,0,100,100)];
// [arrowButton addTarget:self action:#selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[vc addSubview:blog];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
}];
Button Click:
-(void)button:(id)sender{
NSLog(#"press");
[myImageView8 addSubview:awesomeView];
}
UIPanGesture:
myImageView8=[[UIImageView alloc]initWithFrame:CGRectMake(20, 150, 170, 150)];
[self.view addSubview:myImageView8];
myImageView8.userInteractionEnabled=YES;
UIPanGestureRecognizer *pan8=[[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(onPan8:)];
[myImageView8 addGestureRecognizer:pan8];
- (void)onPan8:(UIPanGestureRecognizer *)pan {
// CGPoint offset = [pan translationInView:self.view];
CGPoint offset = [pan translationInView:myImageView8];
CGPoint center = pan.view.center;
center.x += offset.x;
center.y += offset.y;
pan.view.center = center;
[pan setTranslation:CGPointZero inView:myImageView8];
}
set ContentOffset value to the scrollview then it automatically moves to the position
[scroll setContentOffset:CGPointMake(320*index, 0) animated:YES];//index is an integer type value

Strange behaviour when laying out subviews

I am creating a UI that the user can page through multiple views by using a UIScrollView. I create the UIView objects in code and add them to UIScrollView. I use the code below to create the views.
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *brandView = [[UIView alloc] initWithFrame:frame];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 50.0, frame.size.width, 30)];
label.text = [brand objectForKey:#"name"];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
[brandView addSubview:label];
UIImageView *logoImageView = [[UIImageView alloc] init];
logoImageView.frame = CGRectMake(20.0, 100.0, 280.0, 300.0);
[brandView addSubview:logoImageView];
logoImageView.file = [brand objectForKey:#"logo"];
[logoImageView loadInBackground];
Although I give 50.0 for the y coordinate for the label, I can't see the label at all when I run the app. And logoImageView has 100.0 for its top but it appears just below the status bar, as if it had 0.0 for it's y coordinate.
What is the reason for this strange behaviour, am I doing something wrong?
Note: Auto layout for the UIScrollView is disabled on IB.
Below code will work for you !
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * 10;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *brandView = [[UIView alloc] init]; // CHANGE THAT HAS TO BE DONE
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 50.0, self.scrollView.frame.size.width, 30)];
label.text = #"Test";
label.textAlignment = UITextAlignmentCenter; // DEPRECATED IN iOS 6.0
label.backgroundColor = [UIColor redColor];
[brandView addSubview:label];
[self.scrollView addSubview:brandView];

Changing value on a label with UISlider control in WePopoverControler

I am in a process of implementing WePopovercontroller in my app and I have a question on how to implement uislider on it. I am able to show the slider in the UIView and get the action when slider moved but not sure how to set the value of the label in that view. Here is the part of the code
-(void)popoverSliderMoved:(UISlider *) sender{
NSLog(#"slider %f",sender.value);
}
-(IBAction)showSettingsMenu:(UIButton *)sender{
if(!self.popoverSettingsController) {
// Create a label and button for the popover
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setText:#"Bookmark it!"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:UITextAlignmentCenter];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 45, 100, 40)];
[slider addTarget:self action:#selector(popoverSliderMoved:) forControlEvents:UIControlEventValueChanged];
UIFont *font = [UIFont boldSystemFontOfSize:20];
[label setFont:font];
CGSize size = [label.text sizeWithFont:font];
CGRect frame = CGRectMake(0, 0, size.width + 10, size.height + 10); // add a bit of a border around the text
label.frame = frame;
UIButton *button = [[UIButton alloc] initWithFrame:label.frame];
[button addSubview:label];
[button addTarget:self action:#selector(popoverButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIViewController *viewCon = [[UIViewController alloc] init];
[viewCon.view addSubview:slider];
[viewCon.view addSubview:button];
CGRect popOverFrm = CGRectMake(0, 0, frame.size.width + slider.frame.size.width, frame.size.height + slider.frame.size.height);
viewCon.contentSizeForViewInPopover = popOverFrm.size;//CGSizeMake(100, 36);
NSLog(#"Label Frame: %#", NSStringFromCGRect(label.frame));
NSLog(#"Popover size: %#", NSStringFromCGSize(viewCon.contentSizeForViewInPopover));
NSLog(#"ViewCon: %#", NSStringFromCGRect(viewCon.view.frame));
self.popoverSettingsController = [[WEPopoverController alloc] initWithContentViewController:viewCon];
//[self.popoverSettingsController setDelegate:self];
}
if([self.popoverSettingsController isPopoverVisible]) {
[self.popoverSettingsController dismissPopoverAnimated:YES];
// [navPopover setDelegate:nil];
self.popoverSettingsController = nil;
} else {
[self.popoverSettingsController presentPopoverFromRect:CGRectMake(sender.frame.size.width, 0, 200, 57)
inView:self.navigationController.view
permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown
animated:YES];
}
}
Would i need to use a delegate in this case or a property for this viewcontroller to set the label.
Thanks!!!
Yes using a property would be what I would do.