UITextView strange behavior - iphone

Okay I'm lost here.
So have this UIViewController we can call it viewController1 where I add a new editable UITextView for every click on an add button.
I then use UIPanGestureRecognizer so you can drag them where ever you want. This works just fine. Now I want to go to viewController2: [self.navigationController pushViewController:viewController2 animated:YES]; but when I press the back button to return to viewController1 the first UITextView that was created in viewController1 has changed. It's not gone but the text property inside the textview has moved.
This just happens for the first instance of UITextView that I create, any ideas here would be appreciated.
Here's my method for creating the text:
-(void)addTextFieldToScreen
{
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 140, 120)];
textView.text = #"TEXT";
textView.backgroundColor = [UIColor clearColor];
textView.textColor = [UIColor purpleColor];
textView.font = [UIFont fontWithName:#"AmericanTypewriter" size:30];
textView.userInteractionEnabled = YES;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveText:)];
panRecognizer.minimumNumberOfTouches = 1;
panRecognizer.maximumNumberOfTouches = 1;
panRecognizer.delegate = self;
[textView addGestureRecognizer:panRecognizer];
[self.view addSubview:textView];
}
And here's my method for making my textView dragable:
-(void)moveText:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat workingWidth = screenRect.size.width - 40;
CGFloat workingHeight = screenRect.size.height - 80;
if(finalX < 50) {
finalX = 50;
}
else if(finalX > workingWidth ) {
finalX = workingWidth;
}
if(finalY < 80) {
finalY = 80;
}
else if(finalY > workingHeight) {
finalY = workingHeight;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:.20];
[UIView commitAnimations];
}
}
EDIT:
Okay here's whats going on, I add two textviews with content "TEXT" with the "add text" button in the right corner in viewController1:
And then when I come back from viewController2 to viewController1 this is what I see:
As you can see it's only the left textView instance text property that change position, and this is the first one I'm adding.

Related

How to make collage in shape for iPhone application?

I want many collages in some particular shape like heart shape, star shape, etc...
Can any one tell me how can I do this programmatically in my iPhone app with objective c?
Use this code. Here The images are being picked from the PhotoLibrary and then using gestures they are being moved, zoomed etc. I am using All the gestures to help u do virtually any panning, pinching and rotating of any image you put.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
imageCollage = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 150, 150)];
[imageCollage setUserInteractionEnabled:YES];
imageCollage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,imageCollage.frame .size.width, imageCollage.frame.size.height)];
holderView.layer.cornerRadius = 6;
holderView.clipsToBounds = YES;
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageCollage.image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
DeleteImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[DeleteImage setImage:[UIImage imageNamed:#"DeleteRed.png"]];
[holderView addSubview:DeleteImage];
DeleteImage.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[DeleteImage addGestureRecognizer:tapRecognizer];
[BaseView addSubview:holderView];
[Trash setImage:[UIImage imageNamed:#"12456977871712665073hrum_trash.svg.med.png"] forState:UIControlStateNormal];
}
-(void)scale:(id)sender
{
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender
{
[BaseView bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
-(void)move:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 260) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 416) {
finalY = 416;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 416) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 260) {
finalY = 416;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
-(void)tapped:(UIGestureRecognizer *)recogniser
{
NSLog(#"%#",recogniser.view);
[Trash setImage:[UIImage imageNamed:#"trash_bin_recycle_quit_terminate_error_cancel_close_exit.png"] forState:UIControlStateNormal];
[[[recogniser view] superview] removeFromSuperview];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
Any queries about the cod please feel free to ask :)... Happy Coding ..
If you have little experience with Core Graphics and want to programmatically generate those kinds of shapes, I would recommend you use a Mac app called PaintCode.
You can use it to draw your shapes, and it generates the corresponding Objective-C drawing code.

How to increase or decrease the size of UIImageView by dragging the right corner of the edge?

I am doing an app which has a feature that resizing UIImageView by dragging the right corner of the UIImageView's edge like zoom in and zoom out. Also we can rotate the image.
How can I achieve this?
In case of resizing UIImage use SPUserResizableView follow link.
For rotation u will add UIRotationGestureRecognizer to resizableview.
Use this code. Here The images are being picked from the PhotoLibrary and then using gestures they are being moved, zoomed etc. I am using all the gestures to help u do virtually anything like panning, pinching and rotating of any image you put.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
imageCollage = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 150, 150)];
[imageCollage setUserInteractionEnabled:YES];
imageCollage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,imageCollage.frame .size.width, imageCollage.frame.size.height)];
holderView.layer.cornerRadius = 6;
holderView.clipsToBounds = YES;
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageCollage.image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
DeleteImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[DeleteImage setImage:[UIImage imageNamed:#"DeleteRed.png"]];
[holderView addSubview:DeleteImage];
DeleteImage.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[DeleteImage addGestureRecognizer:tapRecognizer];
[BaseView addSubview:holderView];
[Trash setImage:[UIImage imageNamed:#"12456977871712665073hrum_trash.svg.med.png"] forState:UIControlStateNormal];
}
-(void)scale:(id)sender
{
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender
{
[BaseView bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
-(void)move:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 260) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 416) {
finalY = 416;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 416) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 260) {
finalY = 416;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
-(void)tapped:(UIGestureRecognizer *)recogniser
{
NSLog(#"%#",recogniser.view);
[Trash setImage:[UIImage imageNamed:#"trash_bin_recycle_quit_terminate_error_cancel_close_exit.png"] forState:UIControlStateNormal];
[[[recogniser view] superview] removeFromSuperview];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
Any queries about the code please feel free to ask :)... Happy Coding ..

Pinch Zoom in and Zoom Out in Objective c

How to integrate the pinch zoom and zoom out in our apps, i am using imageview on scrollView
and my code is:
- (IBAction)handlePinchGesture:(UIGestureRecognizer *) recognizer {
if(zoomEnable == TRUE)
{
CGFloat factor = [(UIPinchGestureRecognizer *) recognizer scale];
CGFloat lastScaleFactor = 1;
//if the current factor is greater 1 --> zoom in
if (factor > 1) {
scrollView.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1),lastScaleFactor + (factor-1));
scrollView.scrollEnabled = YES;
} else {
[UIView beginAnimations:#"animation" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:scrollView cache:NO];
scrollView.transform = CGAffineTransformMakeScale(1,1);
[UIView commitAnimations];
}
isScrollable = TRUE;
}
}
Its start zooming every time from start i want if i zoom some then again it start when i stop zoom. Any help is highly Appreciated
Thanks;
You don't need to use UIGestureRecognizers, if you're using UIScrollView already. UIScrollView supports pinch to zoom.
For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum (minimumZoomScale) zoom scale must be different.
-(void)zoomingImages{
self.FullSizeScrollView.pagingEnabled =YES;
NSMutableArray *_scrollArray =[[NSMutableArray alloc]init];
// add images to scroll array
[_scrollArray addObject:self.image1];
[_scrollArray addObject:self.image2];
//now call init with frame function given below to set frame for each image in scroll view
for(int i =0 ;i<[_scrollArray count];i++){
ZoomingImageView *_imageScrollView = [[ZoomingImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, 460)];
_imageScrollView.captureView=self;
[self.checkFullSizeScrollView addSubview:_imageScrollView];
[_imageScrollView release];
self.checkFullSizeScrollView.contentSize = CGSizeMake((i*320)+320, 460);
}
[_scrollArray release];
}
#implementation ZoomingImageView
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.maximumZoomScale = 4;
self.minimumZoomScale = 1;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
self.delegate = self;
self.bouncesZoom = NO;
self.currentImageView.clipsToBounds=NO;
self.contentMode =UIViewContentModeScaleAspectFit;
UIImageView *zoomImageView_ =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.currentImageView = zoomImageView_;
[zoomImageView_ release];
self.currentImageView.contentMode =UIViewContentModeScaleAspectFit;
self.currentImageView.userInteractionEnabled = YES;
self.currentImageView.multipleTouchEnabled = YES;
[self addSubview:self.currentImageView];}
return self;}

keyboards hide entering text

I am working on QuickDialog at here
However, my view can not scroll and the keyboard is hiding the entering text. What I am having so far is
- (void)loadView {
self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationItem.hidesBackButton = NO;
NSLog(#"LOAD VIEW");
self.root = [[QRootElement alloc] init] ;
self.root.controllerName = #"CreateAccountViewController";
self.root.grouped = YES;
self.root.title = #"Registration";
[super loadView];
QSection *section1 = [[QSection alloc] init];
// Create title cell
QEntryElement *cell1 = [[QEntryElement alloc] init];
cell1.title = #"Title: ";
cell1.key = #"title";
cell1.placeholder = #"Mr/Mrs/Miss";
cell1.hiddenToolbar = YES;
cell1.autocapitalizationType = UITextAutocapitalizationTypeNone;
cell1.autocorrectionType = UITextAutocorrectionTypeNo;
// Create firstName cell
QEntryElement *cell2 = [[QEntryElement alloc] init];
cell2.title = #"FirstName: ";
cell2.key = #"firstName";
cell2.placeholder = #"Enter your first name";
cell2.hiddenToolbar = YES;
cell2.autocapitalizationType = UITextAutocapitalizationTypeNone;
cell2.autocorrectionType = UITextAutocorrectionTypeNo;
// Create lastName cell
QEntryElement *cell3 = [[QEntryElement alloc] init];
cell3.title = #"LastName: ";
cell3.key = #"lastName";
cell3.placeholder = #"Enter your last name";
cell3.hiddenToolbar = YES;
cell3.autocapitalizationType = UITextAutocapitalizationTypeNone;
cell3.autocorrectionType = UITextAutocorrectionTypeNo;
// Create phone number cell
QEntryElement *cell4 = [[QEntryElement alloc] init];
cell4.title = #"Phone: ";
cell4.key = #"phoneNumber";
cell4.placeholder = #"Enter your phone number";
cell4.hiddenToolbar = YES;
cell4.autocapitalizationType = UITextAutocapitalizationTypeNone;
cell4.autocorrectionType = UITextAutocorrectionTypeNo;
.............................
.............................
[main addElement:cell1];
[main addElement:cell2];
[main addElement:cell3];
[main addElement:cell4];
[main addElement:cell5];
[main addElement:cell6];
[main addElement:cell7];
[self.root addSection:section];
After running it, i realized that the keyboard is hiding the context because I have a long list of input. Also, the view is not scrollable at all
There are an attached image at here that you can see my problem...
![enter image description here][3]
I went thru the sample code and the view is scrollable so that the keyboard is not going to hide any text at all.
Does any body have any ideas about this issue, please help. All comments are welcomed here
Just for this record, since this question was asked, this has been implemented in QuickDialog, so you don't have to worry about it anymore.
One way to solve this problem is to resize the scroll view to [screen height] - [height of keyboard] (this is coming from a similar post that I saw somewhere).
You might want to try that.
There are 2 possible ways.
To change the view height when keyboard opens or closes.
To change the view origin when keyboard opens or closes.
Implementing this code in the two delegate methods of your textField will do what you want. Here is the textFieldDidBeginEditing:
- (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];
}
And here is the textFieldDidEndEditing:
- (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];
[self textFieldShouldReturn:textField];
[UIView commitAnimations];
}
If you want it for the buttons you can just setup an IBAction with the above code.

How can I customize an iOS alert view?

I want to create a custom alert view within my iOS application. For example, I want to put some images in this alert, and change its color.
I know how to create a normal UIAlertView, but is there a way to customize an alert view?
I set up my own UIViewController which I can skin with my own images. I generally only use one or two buttons, so I hide the second button if it's not being used. The view is actually the size of the entire screen, so it blocks touches behind it, but it is mostly transparent, so the background shows through.
When bringing it in, I use a few animations to make it bounce like Apple's alert view. Something like this works:
-(void)initialDelayEnded {
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
self.view.alpha = 1.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/1.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce1AnimationStopped)];
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounce1AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce2AnimationStopped)];
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounce2AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
I have the possibility of a short delay built into the class, so initialDelayEnded is called when that delay is over.
When initializing, I pass in an object and selector I want called when each button is pressed, and then I call the appropriate selector on the object when a button is pressed.
Here's a custom alert view that I wrote which is a drop-in replacement for UIAlertView. You can set a custom background image; it wouldn't be hard to extend to support custom background colors.
https://github.com/TomSwift/TSAlertView
Create one sub class for UIAlertView.
And create common class for Alert View method.
Add 2 below method for it.
#pragma mark Alert View Functions
+(void)alertViewWithYesNo:(NSString *)pstrTitle:(NSString *)pstrMessage:(int)pstrTagId:(id)pDelegate{
UIAlertView *objAlertNotify = [[UIAlertView alloc] init];
[objAlertNotify setDelegate:pDelegate];
[objAlertNotify addButtonWithTitle:#""];
[objAlertNotify addButtonWithTitle:#""];
int intTemp = 1;
for (UIView* view in [objAlertNotify subviews])
{
if ([[[view class] description] isEqualToString:#"UIAlertButton"])
{
UILabel *theTitle = [[UILabel alloc] init];
[theTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:g_AlertFontSize]];
[theTitle setTextColor:[UIColor whiteColor]];
switch (intTemp) {
case 1:
[theTitle setText:#"Yes"];
//[theTitle setTextColor:g_ColorYes];
break;
case 2:
[theTitle setText:#"No"];
//[theTitle setTextColor:g_ColorNo];
break;
}
intTemp++;
[theTitle setBackgroundColor:[UIColor clearColor]];
[theTitle setTextAlignment:UITextAlignmentCenter];
[view addSubview:theTitle];
}
else if ([[[view class] description] isEqualToString:#"UIThreePartButton"])
{
UILabel *theTitle = [[UILabel alloc] init];
[theTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:g_AlertFontSize]];
[theTitle setTextColor:[UIColor whiteColor]];
switch (intTemp) {
case 1:
[theTitle setText:#"Yes"];
//[theTitle setTextColor:g_ColorYes];
break;
case 2:
[theTitle setText:#"No"];
//[theTitle setTextColor:g_ColorNo];
break;
}
intTemp++;
[theTitle setBackgroundColor:[UIColor clearColor]];
[theTitle setTextAlignment:UITextAlignmentCenter];
[view addSubview:theTitle];
}
}
[objAlertNotify setTag:pstrTagId];
[objAlertNotify setTitle:pstrTitle];
[objAlertNotify setMessage:pstrMessage];
[objAlertNotify show];
}
+(void)alertViewBtnText:(UIAlertView *)alertView{
for (UIView* view in [alertView subviews])
{
//NSLog(#"%#", [[view class] description]);
if ([[[view class] description] isEqualToString:#"UIAlertButton"])
{
for (UILabel *lbl in [view subviews])
{
//NSLog(#"%#", [[lbl class] description]);
if ([[[lbl class] description] isEqualToString:#"UILabel"])
{
CGRect frame = [view bounds];
CGSize maximumLabelSize = CGSizeMake(320,480);
CGSize expectedLabelSize = [lbl.text sizeWithFont:lbl.font constrainedToSize:maximumLabelSize lineBreakMode:lbl.lineBreakMode];
CGRect newFrame = lbl.frame;
newFrame.origin.x = newFrame.origin.x - expectedLabelSize.width/2;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
lbl.frame = newFrame;
//frame.size.width = 320.0;
//frame.size.height = 480.0;
lbl.frame = frame;
[lbl setCenter:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)];
}
}
}
else if ([[[view class] description] isEqualToString:#"UIThreePartButton"])
{
for (UILabel *lbl in [view subviews])
{
CGRect frame = [view bounds];
CGSize maximumLabelSize = CGSizeMake(320,480);
CGSize expectedLabelSize = [lbl.text sizeWithFont:lbl.font constrainedToSize:maximumLabelSize lineBreakMode:lbl.lineBreakMode];
CGRect newFrame = lbl.frame;
newFrame.origin.x = newFrame.origin.x - expectedLabelSize.width/2;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
lbl.frame = newFrame;
//frame.size.width = 320.0;
//frame.size.height = 480.0;
lbl.frame = frame;
[lbl setCenter:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)];
}
}
}
}
Now, in whatever class, you are using this custom Alert:
Add below:
#pragma mark UIAlertViewDelegate
-(void)willPresentAlertView:(UIAlertView *)alertView{
if(alertView==objAlertMsg){
/*clsCommonFuncDBAdapter *objclsCommonFuncDBAdapter = [[clsCommonFuncDBAdapter alloc] init];
float newHeight = [objclsCommonFuncDBAdapter getAlertHeightByMessage:alertView.frame.size.width :alertView.message] + [g_AlertExtraHeight intValue];
[objclsCommonFuncDBAdapter release];
//NSLog(#"X = %f, Y = %f, Widht = %f, Height = %f", alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, alertView.frame.size.height);
//[alertView setFrame:CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, 110.0)];
[alertView setFrame:CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, newHeight)];*/
}
[clsCommonFuncDBAdapter alertViewBtnText:alertView];
}
For calling it:
Use like below:
-(void)askForGPSEnable{
[clsCommonFuncDBAdapter alertViewWithYesNo:msgGPSTitle :msgGPSMessage :0 :self];
}
Let me know in case of any difficulty.
You'll need to create your own custom view, and implement some alert view style behaviour, such as displaying modally, dimming the background, animating in and out, etc.
There's no support in the SDK for customising a UIAlertView any further than the text or buttons.
http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html
and
http://www.skylarcantu.com/blog/2009/08/14/custom-uialertview-color-chooser/
You can use above links for custom alerts. I hope these will be helpful for you.
There is very good example on Git hub for custom alert view. It is using UI alert view in core and provides number of methods to customize alert view in different ways