implement scrollview for subview iphone - iphone

How can I include this view in a scrollerView?
I have a subview with different textfields ,and I want to have it in a scrollview so that when a user clicks on a field view get scrolled.
- (UIView *)Form
{
if (!_Form) {
CGRect frame = CGRectMake(0.0, Height, Width, 310.0);
UIView *container = [[UIView alloc] initWithFrame:frame];
CGFloat y = 15.0;
frame = CGRectMake(15.0, y, width, height);
UITextField *field = [[UITextField alloc] initWithFrame:frame];
[
field.placeholder = #"text";
CGFloat spacing = 8.0;
y = frame.origin.y + height + spacing;
frame = CGRectMake(15.0, y, kDeviceWidth - 2*15.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text6";
frame.size.height = 200.0;
y = frame.origin.y + height + space;
frame = CGRectMake(15.0, y, width - 2*15.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text1*";
y = frame.origin.y + height + 16.0;
CGFloat w = (kDeviceWidth - 2 * 15.0) / 2;
frame = CGRectMake(15.0, y, w - 2.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text3*";
frame = CGRectMake(15.0 + w + 2.0, y, w - 2.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text4*";
y = frame.origin.y + height + spacevalue;
frame = CGRectMake(15.0, y, w, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"text5";
y = frame.origin.y + height + 20.0;
frame = CGRectMake((frame.size.width - 192.0) / 2, y, 192.0, 34.0);
y = frame.origin.y + height + 8.0;
frame = CGRectMake((frame.size.width - 192.0) / 2, y, 192.0, 34.0);
}
return _Form;
}
Can you help me with code pls.
Thanks

I have this code:
CGPoint puntoInicial;
-(void) textFieldDidBeginEditing:(UITextField *)textField {
CGPoint pt;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:scrollView];
pt = rc.origin;
puntoInicial = pt;
pt.x = 0;
pt.y -= 250;
[scrollView setContentOffset:pt animated:YES];
textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
}
//Bajar la pantalla cuando se termina de editar
-(void) textFieldDidEndEditing:(UITextField *)textField {
puntoInicial.x = 0;
puntoInicial.y = 0;
[scrollView setContentOffset:puntoInicial animated:YES];
}
Only put this in your code and in the textField -> Connections inspector -> Oulets -> Delegate, put the Files Owner

Related

Zooming a UIScrollView containing a ViewController

I'm trying to zoom and before it was working with a image in the scrollview. Now I'm trying to replace that image with a ViewController and zoom in on the object in that ViewController. It is not working.
viewDidLoad:
- (void)viewDidLoad
{
self.boardView = [[BoardViewController alloc] init];
[self.scrollView addSubview:self.boardView.view];
[self.scrollView setContentSize:(CGSizeMake(320, 320))];
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[self.scrollView addGestureRecognizer:doubleTapRecognizer];
viewForZoomingInScrollView:
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [self.scrollView.subviews objectAtIndex:0];
doubleTapped method: (it is calling this when I put a log in it, it's just not zooming.
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
CGPoint pointInView = [recognizer locationInView:self.boardView.view];
CGFloat newZoomScale = self.scrollView.zoomScale * 3.0f;
if (self.scrollView.zoomScale > self.scrollView.minimumZoomScale){
newZoomScale = MIN(newZoomScale, self.scrollView.minimumZoomScale);
}
else {
newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);
}
CGSize scrollViewSize = self.scrollView.bounds.size;
CGFloat w = scrollViewSize.width / newZoomScale;
CGFloat h = scrollViewSize.height / newZoomScale;
CGFloat x = pointInView.x - (w / 2.0f);
CGFloat y = pointInView.y - (h / 2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self.scrollView zoomToRect:rectToZoomTo animated:YES];
I don't understand what's wrong with it.

add scroll view for a sample form ehich contains few textfields (iphone)

I have a sign up form defined as following and i like to have this form in a scrollview so that if user selects the textfields in the bottom it scrolls.
The thing is that , the sign up form does not cover the whole screen and in the background I still have the main view and I want the textfields to scroll inside the form,without changing the location of the window of the form( I dont want the form view comes up and cover the background).
- (UIView *)Form
{
if (!_Form) {
CGRect frame = CGRectMake(0.0, Height, Width, 310.0);
UIView *container = [[UIView alloc] initWithFrame:frame];
CGFloat y = 15.0;
frame = CGRectMake(15.0, y, width, height);
UITextField *field = [[UITextField alloc] initWithFrame:frame];
[
field.placeholder = #"text1*";
CGFloat spacing = 8.0;
y = frame.origin.y + kDefaultTextFieldHeight + spacing;
frame = CGRectMake(15.0, y, kDeviceWidth - 2*15.0, kDefaultTextFieldHeight);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"Password*";
frame.size.height = 200.0;
y = frame.origin.y + kDefaultTextFieldHeight + spacing;
frame = CGRectMake(15.0, y, kDeviceWidth - 2*15.0, kDefaultTextFieldHeight);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"Confirm Password*";
y = frame.origin.y + kDefaultTextFieldHeight + 16.0;
CGFloat w = (kDeviceWidth - 2 * 15.0) / 2;
frame = CGRectMake(15.0, y, w - 2.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"First Name*";
frame = CGRectMake(15.0 + w + 2.0, y, w - 2.0, height);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"Last Name*";
[container addSubview:field];
y = frame.origin.y + kDefaultTextFieldHeight + spacing;
frame = CGRectMake(15.0, y, w, kDefaultTextFieldHeight);
field = [[UITextField alloc] initWithFrame:frame];
field.placeholder = #"Birthday";
y = frame.origin.y + kDefaultTextFieldHeight + 20.0;
frame = CGRectMake((container.frame.size.width - 192.0) / 2, y, 192.0, 34.0);
y = frame.origin.y + kDefaultTextFieldHeight + 8.0;
frame = CGRectMake((container.frame.size.width - 192.0) / 2, y, 192.0, 34.0);
_Form = container;
}
return _Form;
}
Thanks!
Try and change below code according to fulfill your requirements:
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

How to place arrange images dynamically in a circle

How can i dynamically arrange icons(UIButtons, with backgroundImages) in circle image.SOmething like it should like concentric circles.
I am using the following code,
UIImageView *container = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0f, 299.0f, 298.0f)];
container.image = [UIImage imageNamed:#"AR_RF004_circle.png"];
container.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
[self.view addSubview:container];
NSArray *imagesArray = [[NSArray alloc]initWithObjects:[UIImage imageNamed:#"AR1.png"],[UIImage imageNamed:#"AR2.png"],[UIImage imageNamed:#"AR3.png"],[UIImage imageNamed:#"AR4.png"],[UIImage imageNamed:#"AR5.png"], nil];
int numberOfSections = 5;
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j < numberOfSections; ++j) {
UIButton *sectionLabel = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 128.0f)];
[sectionLabel setBackgroundImage:[imagesArray objectAtIndex:j] forState:UIControlStateNormal];
sectionLabel.layer.anchorPoint = CGPointMake(0.9f, 0.1f);
sectionLabel.layer.position = CGPointMake(container.bounds.size.width/2.0, container.bounds.size.height/2.0); // places anchorPoint of each label directly in the center of the circle.
sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*j);
[container addSubview:sectionLabel];
NSLog(#"section label x and y is %f ,%f",sectionLabel.frame.origin.x,sectionLabel.frame.origin.y);
}
[container release];
Thanks in Advance
You can use this piece of code:
UIButton *currentButton;
int buttonCount = 20;
int radius = 200;
float angleBetweenButtons = (2 * M_PI) / buttonCount;
float x = 0;
float y = 0;
CGAffineTransform rotationTransform;
for (int i = 0; i < buttonCount; i++)
{
// get your button from array or something
currentButton = ...
...
x = radius + cosf(i * angleBetweenButtons) * radius;
y = radius + sinf(i * angleBetweenButtons) * radius;
rotationTransform = CGAffineTransformIdentity;
rotationTransform = CGAffineTransformRotate(rotationTransform, (i * angleBetweenButtons));
currentButton.center = CGPointMake(x, y);
currentButton.transform = rotationTransform;
}
I think you can do something like this
sectionLabel.center = container.center;
I have not checked other code.. Simplest way for this

Why is UIScrollView not bouncing?

I do this:
scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
[self addSubview:scrollView];
NSInteger x = scrollView.bounds.origin.x + Padding, y = scrollView.bounds.origin.y + Padding;
backButton = [[AButtonControl alloc] initWithCaption:#"Back" style:AButtonViewStyleGreen];
backButton.frame = CGRectMake(x, y, backButton.frame.size.width, backButton.frame.size.height);
y += backButton.frame.size.height + Spacing;
[scrollView addSubview:backButton];
ownerNameTextField = [[AFormTextField alloc] initWithFrame:CGRectMake(x, y, 200, 60) label:#"Guest name"];
y += ownerNameTextField.frame.size.height + Spacing;
[scrollView addSubview:ownerNameTextField];
guestListSelectField = [[AFormSelectField alloc] initWithFrame:CGRectMake(x, y, 200, 60) label:#"Guest list"];
y += guestListSelectField.frame.size.height + Spacing;
[scrollView addSubview:guestListSelectField];
referenceUserSelectField = [[AFormSelectField alloc] initWithFrame:CGRectMake(x, y, 200, 60) label:#"Reference user"];
y += referenceUserSelectField.frame.size.height;
[scrollView addSubview:referenceUserSelectField];
scrollView.contentSize = CGSizeMake(self.bounds.size.width, y + Padding);
And it scrolls fine, but the view does not bounce like it is supposed(?) to.
Any ideas?
The underlying problem here is if you set the scrollview's frame/bounds in -layoutSubviews, it doesn't check if you're setting it to the frame/bounds that it already has, and this cancels the bounce. The solution is to do something like
CGFrame scrollFrame = [self calculateFrameForScrollView];
if (!CGRectEqualToRect(scrollFrame, self.myScrollView.frame))
self.myScrollView.frame = scrollFrame;
Found at: UIScrollView and its subclasses won't bounce if nested in custom UIView

How to create grid button on iphone. 10/10 matrix

How to create Grid of Buttons on iphone.
10/10 matrix...
I found NSMatrix for MAC ... Not for iphone...
any alternative way to create grid of button on my view.
#Thanks in advance.
Manually?
int rows = 10;
int cols = 10;
float gridWidth = 320.0;
float gridHeight = 320.0;
float buttonWidth = 28.0;
float buttonHeight = 28.0;
float gapHorizontal = (gridWidth - (buttonWidth * rows)) / (rows + 1);
float gapVertical = (gridHeight - (buttonHeight * cols)) / (cols + 1);
float offsetX;
float offsetY;
int count = 0;
do {
offsetX = gapHorizontal + ((count % rows) * (buttonWidth + gapHorizontal));
offsetY = gapVertical + ((count / rows) * (buttonHeight + gapVertical));
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(offsetX, offsetY, buttonWidth, buttonHeight)];
aView.backgroundColor = [UIColor redColor];
[self.view addSubview:aView];
[aView release];
offsetX+= buttonWidth + gapHorizontal;
count++;
} while(count < rows * cols);
Also check out AQGridView. I guess it's pretty much what you are looking for. And more.
As I usually use this to make grid.. Only you have to just replace with your row and column
#define MatrixRow 3
#define MatrixColumn 3
#define GRID_WIDTH 300
#define GRID_HEIGHT 300
UIView *grid = [[UIView alloc] initWithFrame:CGRectMake(10, 50, GRID_WIDTH, GRID_HEIGHT)];
grid.backgroundColor = [UIColor brownColor];
for (int i = 0; i< (MatrixColumn*MatrixRow); i++) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
view.backgroundColor = [UIColor yellowColor];
CGFloat xOrigin = GRID_WIDTH/(MatrixColumn*2) + (GRID_WIDTH/MatrixColumn)*(i%MatrixColumn);
CGFloat yOrigin = GRID_HEIGHT/(MatrixRow*2) + (GRID_HEIGHT/MatrixRow) *(i/MatrixColumn);
NSLog(#" (\"%.2f\" , \"%.2f\")",xOrigin,yOrigin);
view.center = CGPointMake(xOrigin, yOrigin);
[grid addSubview:view];
}
[self.view addSubview:grid];