rotation animation xcode iphone - xcode

Im trying to rotate an image around in a circle using a simple single view application in xcode. I have a circle image on the main storyboard and a button. Im using the following code, but the circle image drifts down to the right then back up to the left as its spinning. I'm not sure what it's missing.
Thanks for you help.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIImageView *theImageView;
IBOutlet UIButton *theButton;
NSTimer *theTimer;
float angle;
BOOL runStop;
}
#property (atomic, retain) IBOutlet UIImageView *theImageView;
#property (atomic, retain) IBOutlet UIButton *theButton;
-(IBAction)runRoulette:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize theButton, theImageView;
- (void)viewDidLoad
{
angle = 0;
runStop = FALSE;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)rotateRoulette
{
theImageView.center = CGPointMake(self.theImageView.center.x, self.theImageView.center.y);
theImageView.transform=CGAffineTransformMakeRotation (angle);
angle+=0.001;
}
-(IBAction)runRoulette:(id)sender
{
if(!runStop)
{
theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/600.0 target:self selector:#selector(rotateRoulette) userInfo:nil repeats:YES];
}
else
{
[theTimer invalidate];
theTimer = nil;
}
runStop = !runStop;
}
#end

I found a very simple way to make my animation work how i wanted it too -
theImageView.center = CGPointMake(160.0, 364.0);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5];
//theImageView.center=CGPointMake(self.theImageView.center.x, self.theImageView.center.y);
theImageView.transform = CGAffineTransformMakeRotation(M_PI/2.65);
[UIView commitAnimations];
It spins the theImageView M_PI/2.65 (M_PI/2.65 is the roataion amount) for a count of 5.

Related

NSButton does not respond to MouseDown Events if it is a part of custom NSView

I have an OS X application with a main view (i.e., self.view) that contains an NSScrollView and the NSScrollView contains a custom NSView. The custom NSView (flippedNSView) is just an NSView custom class FlippedNSView with a -(BOOL)isFlipped { return YES; }
I have a function that creates a NSButton that is called in the ViewDidLoad.
If button is added to self.view, button works fine
If button is added to flippedNSView, button does not respond.
NSTextViews work fine and respond to MouseDown events, although they do not respond to text selection. Only the buttons do not respond.
Any ideas?
AppDelegate.h
#import "CodeObjViewController.h"
#interface AppDelegate : NSObject <NSApplicationDelegate> {
FlippedView *flippedNSView;
}
// Windows are defined in MainMenu.xib with the following hierarchy
// Window
// |_ View
// |_ flippedNSView
// |_ scrollView
// |_ myCustomView
#property (assign) IBOutlet NSWindow *window;
#property (strong) IBOutlet NSViewController *myViewController;
#property (weak) IBOutlet NSView *myCustomView;
#property(readwrite, strong, nonatomic) IBOutlet NSScrollView* scrollView;
#property(readwrite, strong, nonatomic) IBOutlet FlippedView * flippedNSView;
#end
AppDelegate.m
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
#synthesize scrollView;
#synthesize flippedNSView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
_myViewController = [[CodeObjViewController alloc] initWithNibName:#"customViewController" bundle:nil];
[_myCustomView addSubview:[_myViewController view]];
[[_myViewController view] setFrame:[_myCustomView bounds]];
[scrollView setDocumentView: flippedNSView];
}
customViewController.h
#interface FlippedView : NSView
#end
//----------------------------------------------------------
#interface CodeObjViewController : NSViewController {
FlippedView *flippedNSView;
NSScrollView *scrollView;
}
customViewController.m
#implementation FlippedView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
return self;
}
//----------------------------------------------------------
- (BOOL)isFlipped {
return YES;
}
#end
//----------------------------------------------------------
#implementation customViewController
// Custom View Controller
-(void) viewDidLoad {
// The scroll and flipped views are defined on menu.xib and declared on the AppDelegate.
AppDelegate *theAppDelegate = (AppDelegate*) [NSApplication sharedApplication].delegate;
flippedNSView = theAppDelegate.flippedNSView;
scrollView = theAppDelegate.scrollView;
NSButton *button = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,10,10)];
[self.view addSubview:helpButton]; // THIS RESPONDS TO MOUSE DOWN
[flippedNSView addSubview:helpButton]; // THIS DOES NOT RESPOND TO MOUSE DOWN
[button setAction:#selector(buttonClicked:)];
[button setTarget:self];
}
-(void) buttonClicked : (id) sender {
NSLog(#"Button clicked");
}

showsUserLocation action attached to button not working

I am having problems with a map view in my app. I have created a button that when clicked should show users location on the map, but nothing happens (no error messages occur).
I believe the issue may lie in the way I've written the delegates. The code from the relevant .h and .m files is below:
mapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface mapViewController : UIViewController {
MKMapView *mapview;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getCurrentLocation:(id)sender;
#property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
#end
mapViewController.m
#import "mapViewController.h"
#interface mapViewController ()
#end
#implementation mapViewController {
CLLocationManager *locationManager;
}
#synthesize mapview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview.mapType = MKMapTypeStandard;
break;
case 1:
mapview.mapType = MKMapTypeSatellite;
break;
case 2:
mapview.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(IBAction)getCurrentLocation:(id)sender {
mapview.showsUserLocation = YES;
}
#end
Any help would be greatly appreciated, thanks
You have to implement the MapKit delegates. (Make sure you add the delegate signature in .h of your view controller)
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = userLocation.coordinate.latitude;
location.longitude = userLocation.coordinate.longitude;
region.span = span;
region.center = location;
[mapView setRegion:region animated:YES];
}
Extra: To handle App is on foreground or not:
We add 2 event observers to observe the App is entering background / returning active:
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appToBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)appToBackground{
[mapview setShowsUserLocation:NO];
}
- (void)appReturnsActive{
[mapview setShowsUserLocation:YES];
}

Receiver is a forward class and corresponding #interface may not exist

thanks for taking the time to read my problem and help me:)
I am building a TabView project and on tab 3 of the app I have a row of albums, in album 3, page 1 the nextButton takes the user to page 2 of the album. Page 2 has more buttons, including a backPage(get back to page 1) and a backButton(to go back to the album selection page).
ERROR:
A3Page2ViewController.m:92:49: Property 'view' cannot be found in forward class object 'A3Page1ViewController'
WARNING:
A3Page2ViewController.m:91:28: Receiver 'A3Page1ViewController' is a forward class and corresponding #interface may not exist
I have searched this site and others and most of the answers are in relation to how #Class should be used in the header filw and #import ".h" should only be used in the .m file. I have checked my code and I was following the rules, so why does it still not work?
THIS IS WHAT I HAVE DONE: This is the function(in A3Page2ViewController) that is apparently making the error...
-(IBAction)backPage:(id)sender
{
a3Page1ViewController = [[A3Page1ViewController alloc]initWithNibName:#"A3Page1ViewController"bundle:nil];
[self.view addSubview:a3Page1ViewController.view];
}
Please note that when commented out, the app runs FINE all the views are loaded, all buttons including backButton work. When uncommented it just refuses to build, with the errors.
Below I list both A3Page1ViewController and A3Page2ViewController both the .h and .m files.
You can see that I have the #class and #import ".h" in the correct places.
WEIRDNESS!!
I actually have the exact same code WORKING in the 2nd tab, I use it to switch between views with out any problem. SO WHY DOES IT NOT WORK HERE?!?!
PLEASE HELP I AM STUCK ATM AND I DON'T KNOW WHY:/
A3Page1ViewController.h
#import <UIKit/UIKit.h>
#class A3Page2ViewController;
#class ThirdViewController;
#class A3P1;
#class A3P2;
#class A3P3;
#interface A3Page1ViewController : UIViewController {
A3Page2ViewController*a3Page2ViewController;
ThirdViewController*thirdViewController;
A3P1*A3P1;
A3P2*A3P2;
A3P3*A3P3;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *nextButton;
UIButton *backButton;
}
#property(nonatomic,retain) IBOutlet A3Page2ViewController *a3Page2ViewController;
#property(nonatomic,retain) IBOutlet ThirdViewController *thirdViewController;
#property(nonatomic,retain) IBOutlet A3P1 *a3P1;
#property(nonatomic,retain) IBOutlet A3P2 *a3P2;
#property(nonatomic,retain) IBOutlet A3P3 *a3P3;
#property (nonatomic, retain) IBOutlet UILabel *logoLabel;
#property (nonatomic, retain) IBOutlet UILabel *descriptionLabel;
#property (nonatomic, retain) IBOutlet UILabel *copyrightLabel;
#property(nonatomic,retain) IBOutlet UIButton *button1;
#property(nonatomic,retain) IBOutlet UIButton *button2;
#property(nonatomic,retain) IBOutlet UIButton *button3;
#property(nonatomic,retain) IBOutlet UIButton *nextButton;
#property(nonatomic,retain) IBOutlet UIButton *backButton;
-(IBAction)FirstButton:(id)sender;
-(IBAction)SecondButton:(id)sender;
-(IBAction)ThirdButton:(id)sender;
-(IBAction)nextPage:(id)sender;
-(IBAction)backButton:(id)sender;
#end
A3Page1ViewController.m
#import "A3Page1ViewController.h"
#import "FXLabel.h"
#import <QuartzCore/QuartzCore.h>
#import "A3Page2ViewController.h"
#import "ThirdViewController.h"
#import "A3P1.h"
#import "A3P2.h"
#import "A3P3.h"
#implementation A3Page1ViewController
#synthesize a3Page2ViewController,thirdViewController,a3P1 ,a3P2 ,a3P3 , logoLabel, descriptionLabel, button1,button2,button3,nextButton,backButton, copyrightLabel;
-(UILabel*)createLabelWithFrame:(CGRect)frame andFontSize:(float)fontSize andText:(NSString*)text
{
UILabel* label = [[UILabel alloc] initWithFrame:frame];
[label setFont:[UIFont systemFontOfSize:fontSize]];
[label setTextColor:[UIColor whiteColor]];
[label setShadowColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0, -1)];
//[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:text];
return label;
}
-(IBAction)FirstButton:(id)sender
{
a3P1 = [[A3P1 alloc]initWithNibName:#"A3P1"bundle:nil];
[self.view addSubview:a3P1.view];
}
-(IBAction)SecondButton:(id)sender
{
a3P2 = [[A3P2 alloc]initWithNibName:#"A3P2"bundle:nil];
[self.view addSubview:a3P2.view];
}
-(IBAction)ThirdButton:(id)sender
{
a3P3 = [[A3P3 alloc]initWithNibName:#"A3P3"bundle:nil];
[self.view addSubview:a3P3.view];
}
-(IBAction)backButton:(id)sender
{
thirdViewController = [[ThirdViewController alloc]initWithNibName:#"ThirdView"bundle:nil];
[self.view addSubview:thirdViewController.view];
}
-(IBAction)nextPage:(id)sender
{
a3Page2ViewController = [[A3Page2ViewController alloc]initWithNibName:#"A3Page2ViewController"bundle:nil];
[self.view addSubview:a3Page2ViewController.view];
}
- (void)viewDidLoad
{
//CGRectMake(x,y,width,height);
// Do any additional setup after loading the view, typically from a nib.
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
A3Page2ViewController.h
#class A3Page1ViewController;
#class ThirdViewController;
#class A3P10;
#class A3P11;
#class A3P12;
#interface A3Page2ViewController : UIViewController {
A3Page1ViewController*a3Page1ViewController;
ThirdViewController*thirdViewController;
A3P10*a3P10;
A3P11*a3P11;
A3P12*a3P12;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *backButton;
UIButton *backPage;
}
#property(nonatomic,retain) IBOutlet A3Page1ViewController *a3Page1ViewController;
#property(nonatomic,retain) IBOutlet ThirdViewController *thirdViewController;
#property(nonatomic,retain) IBOutlet A3P10 *a3P10;
#property(nonatomic,retain) IBOutlet A3P11 *a3P11;
#property(nonatomic,retain) IBOutlet A3P12 *a3P12;
#property(nonatomic,retain) IBOutlet UIButton *button1;
#property(nonatomic,retain) IBOutlet UIButton *button2;
#property(nonatomic,retain) IBOutlet UIButton *button3;
#property(nonatomic,retain) IBOutlet UIButton *backButton;
#property(nonatomic,retain) IBOutlet UIButton *backPage;
-(IBAction)FirstButton:(id)sender;
-(IBAction)SecondButton:(id)sender;
-(IBAction)ThirdButton:(id)sender;
-(IBAction)backButton:(id)sender;
-(IBAction)backPage:(id)sender;
#end
A3Page2ViewController.m
#import "A3Page1ViewController.h"
#import "FXLabel.h"
#import <QuartzCore/QuartzCore.h>
#import "A3Page2ViewController.h"
#import "ThirdViewController.h"
#import "A3P10.h"
#import "A3P11.h"
#import "A3P12.h"
#implementation A3Page2ViewController
#synthesize thirdViewController,a3Page1ViewController,a3P10 ,a3P11 ,a3P12, backButton,backPage, button1,button2,button3;
-(UILabel*)createLabelWithFrame:(CGRect)frame andFontSize:(float)fontSize andText:(NSString*)text
{
UILabel* label = [[UILabel alloc] initWithFrame:frame];
[label setFont:[UIFont systemFontOfSize:fontSize]];
[label setTextColor:[UIColor whiteColor]];
[label setShadowColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0, -1)];
// [label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:text];
return label;
}
-(IBAction)FirstButton:(id)sender
{
a3P10 = [[A3P10 alloc]initWithNibName:#"A3P10"bundle:nil];
[self.view addSubview:a3P10.view];
}
-(IBAction)SecondButton:(id)sender
{
a3P11 = [[A3P11 alloc]initWithNibName:#"A3P11"bundle:nil];
[self.view addSubview:a3P11.view];
}
-(IBAction)ThirdButton:(id)sender
{
a3P12 = [[A3P12 alloc]initWithNibName:#"A3P12"bundle:nil];
[self.view addSubview:a3P12.view];
}
-(IBAction)backButton:(id)sender
{
thirdViewController = [[ThirdViewController alloc]initWithNibName:#"ThirdView"bundle:nil];
[self.view addSubview:thirdViewController.view];
}
-(IBAction)backPage:(id)sender
{
//*****Alleged Error/Warning causing code=[********
a3Page1ViewController = [[A3Page1ViewController alloc]initWithNibName:#"A3Page1ViewController"bundle:nil];
[self.view addSubview:a3Page1ViewController.view];
}
- (void)viewDidLoad
{
//CGRectMake(x,y,width,height);
// Do any additional setup after loading the view, typically from a nib.
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end

Save UIImageView When Unload A View - Xcode 4.5.1

Creating an app on Xcode 4.5.1 and wondering how do i save the current image in the specified UIImageView when i navigate to a different view. Then when i return to the View it loads up the saved image into the UIImageView?
Thank a lot, appreciate it :D
The code I've posted below involves passing a uiimage to a second view controller. It also includes a button on each controller to move back and forth between the two. The saved image will stay in the uiimageview when you switch to and from the views.
SecondView.h
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController {
IBOutlet UIImageView *yourphotoview;
UIImage *yourimage;
}
#property (retain, nonatomic) UIImage *yourimage;
- (IBAction)back;
#end
SecondView.m
#import "ViewController.h"
#import "SecondView.h"
#interface SecondView ()
#end
#implementation SecondView
#synthesize yourimage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
yourphotoview.image = image;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)back {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)dealloc {
[image release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "SecondView.h"
#interface ViewController : UIViewController
{
IBOutlet UISlider *compression;
IBOutlet UISwitch *smurferderp;
SecondView *SecondViewdata;
}
#property (retain, nonatomic) SecondView *SecondViewdata;
#property (retain, nonatomic) IBOutlet UIImageView *theimage;
- (IBAction)switchview:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "SecondView.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize SecondViewdata, theimage;
- (IBAction)switchview:(id)sender {
SecondView *secondview = [self.storyboard instantiateViewControllerWithIdentifier:#"rr"];
self.SecondViewdata = secondview;
SecondViewdata.yourimage = theimage.image;
[self presentViewController: secondview animated:YES completion:NULL];
}
- (void)dealloc {
[theimage release];
[super dealloc];
}
#end

Scrolling content hidden by keyboard. xcode 4.3.2

Using xcode 4.3.2. I hope this is the last time I have to beg for help.
I have a screen with 4 text fields. The bottom 2 are hidden by the keyboard when editing. I want to be able to scroll down so these 2 hidden fields can be edited. I have pasted my .m and .h files. something is not quite right.
Viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollview;
#property (strong, nonatomic) IBOutlet UITextField *TextField1;
#property (strong, nonatomic) IBOutlet UITextField *TextField2;
#property (strong, nonatomic) IBOutlet UITextField *TextField3;
#property (strong, nonatomic) IBOutlet UITextField *TextField4;
#property (strong, nonatomic) IBOutlet UITextField *activefield;
- (IBAction)HideKeyBoard:(id)sender;
viewcontroler.m
#interface ViewController ()
#end
#implementation ViewController
#synthesize scrollview;
#synthesize TextField1;
#synthesize TextField2;
#synthesize TextField3;
#synthesize TextField4;
#synthesize activefield;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activefield = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activefield = nil;
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]
CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollview.contentInset = contentInsets;
scrollview.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activefield.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0,
activefield.frame.origin.y-kbSize.height);
[scrollview setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollview.contentInset = contentInsets;
scrollview.scrollIndicatorInsets = contentInsets;
}
- (void)viewDidUnload
{
[self setScrollview:nil];
[self setActivefield:nil];
[self setTextField1:nil];
[self setTextField2:nil];
[self setTextField3:nil];
[self setTextField4:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)HideKeyBoard:(id)sender {
}
#end
Sliding UITextFields around to avoid the keyboard
I have faced this problem too. The link above really helped me. I hope that it will help you too
In the .h file you have to create a property like this
#property CGFloat animatedDistance;
then in the .m file you have to add this:
#synthesize animatedDistance;
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;
then you have to add this method:
- (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;
}
That's it. Good luck : )

Resources