iOS SDK: Moving the button into the center of screen by code - ios

I want to move a button to the center of the screen by code. I saw somewhere it is just few lines of code, but could not find them.

This centers the button in its superview:
CGRect bounds = button.superview.bounds;
button.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));

This should do it:
yourButton.frame = CGRectMake(self.view.frame.size.width/2 - yourButton.frame.size.width/2, self.view.frame.size.height/2 - yourButton.frame.size.height/2, yourButton.frame.size.width, yourButton.frame.size.height);

Here is more simple approach:
yourButton.center = self.view.center;

Both rooster117 and DavidNg answers seem correct. Just to add one more "option" to you, if you want to do this animated, you should do:
NSTimeInterval seconds = 1.0;
[UIView animateWithDuration:seconds animations:^{
//here you should write any reframing/repositioning code
}];

There are some ways to do it without writing any code.
If you you want to try to place the button into the centre of screen by using interface settings, you can view my clip.

bntTimeEtrySave.Frame =new CoreGraphics.CGRect (
this.View.Center.X-((bntTimeEtrySave.Bounds.Size.Width)/2),
bntTimeEtrySave.Frame.Y,bntTimeEtrySave.Bounds.Size.Width,
bntTimeEtrySave.Bounds.Size.Height);

Related

Why the animation is different when animation code is in keyboard show method?

I want to add an animation when keyboard is showing, I try to add animation like this
- (void)keyboardWillShow:(NSNotification *)notification {
_inputTextView.center = CGPointMake(300, 500);
[UIView animateWithDuration:4 animations:^{
_inputTextView.center = CGPointMake(300, 100);
} completion:nil];
}
before animation I try to set textView center to (300, 500), but it seems not work.
If I move the code to button click method it works as I want, it just like the system has changed my animation when in keyboard method, anyone know the reason and tell me why? thanks!

UIView not changing position in animateWithDuration

I have two UIViews (My bad it is a UIView and a UIButton) which I am animating at the same time. I originally had a view and a containerView which would animate just fine and worked like a charm.
Now only one of my UIViews will move/animate in animateWithDuration even though through debugging the frame of the other view says that it is in a position it is not.
CGRect rect = self.toggle.frame;
CGRect tabRect = self.tabButton.frame;
rect.origin.x = rect.origin.x - rect.size.width;
NSLog(#"%f Before",tabRect.origin.x);
tabRect.origin.x = tabRect.origin.x - rect.size.width;
NSLog(#"%f After", tabRect.origin.x);
[UIView animateWithDuration:0.3 animations:^{ // animate the following:
self.toggle.frame = rect; // move to new location
self.tabButton.frame = tabRect;
}];
NSLog(#"%f AfterAnimation", tabButton.frame.origin.x);
The toggle view moves fine, but the tabButton view does not animate or move. The strange thing is that both the "After" and "AfterAnimation" debugging code returns the same value, which suggests the frame has indeed moved. Is there a specific reason that this will not work when toggle is a UIView when it would work as a UIContainerView?
Note that if I remove the line
self.toggle.frame = rect;
tabButton will animate correctly, but if I move toggle, tabButton will not move regardless of whether it is first in the animation block or second.
Edit: I have tried moving them into separate blocks and to change the center point rather than the frame, to no avail. It seems that if the toggle view moves, the tabButton will not move.
Edit 2: The pictorial evidence.{
In the following screenshots tabButton bg is green and toggle bg is red.
Above: Initial position (toggle is off-screen) correct position
Above: The problem in question toggle is correct tabButton is not
Above: When self.toggle.frame = rect is commented out (tabButton correct, toggle not)
}
Edit 3: It's even worse than I feared.{
I have done a few more tests and even if I take the toggle change out of the animation block to make it an instant thing, the tabButton will still not animate. This makes me think the tabButton may just fundamentally dislike the toggle view and/or myself so will not move just to spite me.
}
Edit 4:{
If I change the tabButton animation to tabButton.frame = CGRectMake(10,10,100,100) the View snaps instantly to that location and animates back to its original position in the same time as the animation duration.
}
I better add more bookkeeping/TLDR information in case things aren't clear.
toggle is an instance of ToggleDraw which is a subview of UIView which I created.
tabButton is a UIButton which is part of my IB viewController and a property of the class
Both toggle and tabButton are subviews of self.view
The animations will work individually with no modifications to the logic of the rects but will not work if they are animated at the same time
toggle animation seems to take precedence over tabButton animation regardless of the order
I had a problem with the animation of an UIView created in IB (the animation didn't start from the current position of the view, and ended in the initial position).
All worked fine after sending layoutIfNeeded() to the underlaying view before the animation block:
self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5) { () -> Void in
...
I think it is not a problem about a UIView Animation. Maybe your toggle posiztion is related to your tabButton. For a try, your can set toggle frame to a rect lick (10, 10, 100,100), then check the result.
I've created an example of what you describe and everything seems to work fine. This is what I used:
UIView *toggle = [[UIView alloc] initWithFrame:CGRectMake(320, 64, 100, 100)];
[toggle setBackgroundColor:[UIColor redColor]];
[self.view addSubview:toggle];
UIButton *tabButton = [[UIButton alloc] initWithFrame:CGRectMake(220, 64, 100, 100)];
[tabButton setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:tabButton];
CGRect rect = toggle.frame;
CGRect tabRect = tabButton.frame;
rect.origin.x = rect.origin.x - rect.size.width;
NSLog(#"%f Before",tabRect.origin.x);
tabRect.origin.x = tabRect.origin.x - rect.size.width;
NSLog(#"%f After", tabRect.origin.x);
[UIView animateWithDuration:1 animations:^{ // animate the following:
toggle.frame = rect; // move to new location
tabButton.frame = tabRect;
}];
What I can suggest is to make sure that the code is being ran on mainthread:
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.3 animations:^{
self.toggle.frame = rect; // move to new location
self.tabButton.frame = tabRect;
}];
});
Also take into account that the log you have after the animation code is incorrect as it won't run after the animation, but rather right next to asking for the animation.
If you want code to run after the animation you should use:
[UIView animateWithDuration:0.3 animations:^{
self.toggle.frame = rect; // move to new location
self.tabButton.frame = tabRect;
} completion:^(BOOL finished){
NSLog(#"Finished animating!");
}];
I have found a solution to the problem. If I initialise the UIButton (tabButton) programmatically rather than through the interface builder, both views will animate simultaneously.
This however seems very hacky to me, kind of like putting a bandaid over a missing foot and hoping it will sort itself out.
I could not work out the root cause of the problem but at least I could avoid the symptoms.
If anyone knows why the views would not animate when the button was made in the interface builder post an answer here, I am interested in knowing the reason behind this.
Thanks for your help everyone.

How to center a UIButton after device is rotated in iOS

I have created a UIButton programmatically and horizontally centered it using the following code
button.center = CGPointMake(self.view.center.x, 50);
When device is rotated, it's no longer in the center. How can I fix this problem? Thanks in advance.
Kevin is correct, but a better solution would be to set the button's center in your view controller's willAnimateRotationToInterfaceOrientation:duration: method.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
button.center = CGPointMake(self.view.center.x, 50);
}
He uses the didRotateFromInterfaceOrientation:fromInterfaceOrientation method, which, as the Apple documentation states "this method might be used to reenable view interactions, start media playback again, or turn on expensive drawing or live updates."
In the case of laying out subviews, the willAnimateRotationToInterfaceOrientation:duration: will provide a smoother transition, as it is called from within the rotation's animation block.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
button.center = CGPointMake(self.view.center.x, 50);
}

Move and Rotate UIButtons

I have one UIbutton. I want it to move from point A to point B. While going, that button have to do one full 360' rotation.
After some event, I want that UIButton to come back to its position, this time rotating in the reverse direction.
I wrote this code, but it didnt work. I'm seeing the button rotate in unexpected ways. What am I missing here?
To move:
[UIView animateWithDuration:0.3 animations:^{
CGAffineTransform rotate360AntiClockWise = CGAffineTransformMakeRotation(M_PI_2);
CGRect initialFrame = button.frame;
[button setTransform:rotate360AntiClockWise];
[button setFrame:CGRectMake(initialFrame.origin.x - 140.0, initialFrame.origin.y, initialFrame.size.width, initialFrame.size.height)];
}];
To comeback to the same position
[UIView animateWithDuration:0.4 animations:^{
CGAffineTransform rotate360ClockWise = CGAffineTransformMakeRotation(-M_PI_2);
[button setFrame:originalFrame]; //for movement
[button setTransform:rotate360ClockWise];
}];
}
I can't try it at the moment, but if I remember correctly using the frame causes problems when applying transformations. Have you tried setting the center of the button?
[button setCenter:newCenter]
Where newCenter is a corresponding CGPoint.
The simplest way to do a rotation and a transform without messing up the Transformation matrix of the view is:
place the button inside of a view (moveView) with the same frame-size with origin 0|0
when you want to do a rotation, rotate the button
when you want to move the button, translate the moveView
Please do this in 3 steps
CGAffineTransform matOne = CGAffineTransformMakeTranslation(x_offset, y_offset);
CGAffineTransform matTwo = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform matResult = CGAffineTransformConcat(matOne, matTwo);
// Finally
[button setTransform:matResult];
I hope this helps you. Please make sure Concat has matOne as the first parameter.
Also this is a possible duplicate for another question:
translation after rotation view using CGAffine

resizing a UIControl in an animated fashion

I have created a button which I want to expand/contract. I found a way to get the x,y coordinates along with width and height. Using these I call CGRectMake function to redraw the button. I am trying to figure out how to redraw in an animated fashion. Below is my attempt but I am almost sure that there are better ways of doing this. Any comments?
pointX = navigateBtn.frame.origin.x;
pointY = navigateBtn.frame.origin.y;
height = CGRectGetHeight(navigateBtn.frame);
width = CGRectGetWidth(navigateBtn.frame);
for (int i=1;i<=800000;i++) {
int k=i/100000;
navigateBtn.frame=CGRectMake(pointX, pointY, width, height*k);
}
Thanks in advance.
All you need to know is what the destination rect will be and then you can animate like this
[UIView animateWithDuration:0.25f
animations:^{
navigateBtn.frame = endFrame;
}];
No. Don't try to animate things yourself.
Use UIView animation. A good tutorial is available from Ray Wenderlich: http://www.raywenderlich.com/2454/how-to-use-uiview-animation-tutorial
But to be perfectly honest you should probably start with an introductory iOS programming book. The way you phrased your question makes me think that you still have a lot of fundamentals to learn.

Resources