Scale/Rotate a button in interface - iphone

I need to rotate a button by 30 degrees in iphones sdk interface builder, how do you do that?

You can't do it in Interface Builder, but the code is pretty straightforward.
Make sure you've connected the button in IB to an IBOutlet of type UIButton* (let's call it myButton). Then once the nib has been loaded (for example, in your viewDidLoad method) you can use something like this:
#define RADIANS(degrees) ((degrees * M_PI) / 180.0)
CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
RADIANS(30.0));
myButton.transform = rotateTransform;

I'm not sure that you can do it directly. You might be able to do it if you were drawing your own buttons via CoreGraphics layers.
Core Animation Layers

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
button.transform=CGAffineTransformMakeRotation((0.0174532925)*30);
//put the -ve sign before 30 if you want to rotate the button in the anticlockwise else use this one
[UIView commitAnimations];

Related

Customized Animation in iphone

I am creating Application which have so many Customization. Customization in the sense, I want to give new look to my application. In the part of customization I want to add THIS kind of animation inside my application.
i have searched over internet finally i found so many 3d animations. But those results are not satisfy my needs.
Suggest me some piece of code to make that animation possible.
If u provide any source code, That will be really helpful for me.
This is exactly you want.Set your view like this.
_transitionFrame is label outlet.That label is placed on your display view.
Include QuartzCore framework and define a method to convert degree to radians like this
#import <QuartzCore/QuartzCore.h>
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
In view did load Method write following code.
- (void)viewDidLoad
{
CGRect preFrame=_transitionFrame.frame;
preFrame.origin.y+=preFrame.size.height/2;
[_transitionFrame setFrame:preFrame];
[UIView beginAnimations:nil context:nil];
CATransform3D _3Dt = CATransform3DRotate(_transitionFrame.layer.transform,DEGREES_TO_RADIANS(90), 1.0, 0.0,0.0);
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:100];
[UIView setAnimationDuration:1];
[_transitionFrame.layer setAnchorPoint:CGPointMake(0.5, 1)];
_transitionFrame.layer.transform=_3Dt;
[UIView commitAnimations];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

Rotating button when downloading data from server

I want to rotate a UIButton when connection start and want to stop rotating when connection stop.
So my question is how to start or stop rotation on UIButton.
I use the following code for rotation.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
// (180 * M_PI) / 180 == M_PI, so just use M_PI
btn.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];
You can remove animations from a view by calling removeAllAnimations
[btn.layer removeAllAnimations];
Make sure you import QuartzCore before accessing layer properties, otherwise you may get a warning.
#import <QuartzCore/QuartzCore.h>
To make the view animate continuously use the + (void)setAnimationRepeatCount:(float)repeatCount method on UIView and set it to some arbitrary high number.
[UIView setAnimationRepeatCount:5000];

change background image in a cell when iPhone rotates

My tableview has 4 cells, and each cells has one different picture as background.
When I rotate the iPhone, the height of the rows change, and also the pictures.
Is really easy to change picture if the position is in landscape or in portrait, but the picture change when the iPhone is already rotated, not while is rotating!
What's the problem?
For example: the iPhone is in portrait and the user rotates it in landscape.
That there is a very ugly effect, because the pictures change just when the iPhone is in landscape, and for a fraction of a second the user sees the portrait picture deformed (before that it changes).
How can I solve it?
Thanks
The easy way:
Implement willAnimateRotationToInterfaceOrientation in your view controller. Inside this method you can reorient your pictures and they'll automatically be animated. There's a short overview this and other available interface orientation methods at http://www.dizzey.com/development/ios/handling-layout-on-uiinterfaceorientation-change/
Pointlessly difficult way:
You could manually track acceleration and write your own animation blocks.
Set your view controller as a UIAccelerometerDelegate. Reference the sharedAccelerometer as follows.
accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f/60.0f;
Then implement this delegate method. This will be run every time the accelerometer notices acceleration. Tracking acceleration in the X axis will give you landscape/portrait orientation.
#pragma mark UIAccelerometer delegate method
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
if(acceleration.x > 0.8) //landscape right
{
[UIView beginAnimations:#"text spin" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.2];
//do some animation to landscape right
[UIView commitAnimations];
}
if(acceleration.x < -0.8) //landscape left
{
[UIView beginAnimations:#"text spin" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.2];
//do some animation to landscape left
[UIView commitAnimations];
}
if(acceleration.x < 0.2 && acceleration.x > -0.2) //portrait
{
[UIView beginAnimations:#"text spin" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.2];
//do some animation to portrait view
[UIView commitAnimations];
}
In a situation like this, during a rotation I tend to just use the .hidden property on elements (myObject.hidden = YES) and then restore them when the rotation is complete.
I imagine your picture frames have more room in landscape view - another suggestion if this is true, is to keep them the same size in both orientations, then you wouldn't need to hide them during a rotation, for example.
Hope this helps!

Difficulties in detecting touch while animating an imageview

I am trying to animate an image using following code:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:40];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGAffineTransform rotate = CGAffineTransformMakeRotation(0.0);
CGAffineTransform moveRight = CGAffineTransformMakeTranslation(0, 0);
CGAffineTransform firstHalf = CGAffineTransformConcat(rotate, moveRight);
CGAffineTransform zoomIn = CGAffineTransformMakeScale(2,2);
CGAffineTransform transform = CGAffineTransformConcat(zoomIn, firstHalf);
fimage.transform = transform;
[UIView commitAnimations];
I have UIScrollView which has one subview UIImageView and fimage is that UIImageView. But when i try to detect the touch on it i am not able to do that. Can any one help me? Is is possible to detect touches on UIScrollView.
The main application is to display images in panning and when user clicks on it the web page containing that image should be loaded in next web view.
How can I achieve this?
I tested your code and it seems to be working without hiccups. It is likely that the image view object has its userInteractionEnabled set to NO. Set it to YES to make touches work.
Check this to see how you can load images in UIWebView object. As for panning, you should try out the UIPanGestureRecognizer if you aren't already.

UISegmentedControl customization

I have a UISegmentedControl on one of my pages. I want an editbox to appear when a segment is clicked right below the clicked segment. I would like it to be animated (slide in or something)
Is this possible? What would be the best way to do this?
Damn.I forgot to mention all this action is going to occur within a cell and not a simple view.
You may try UIView animation.
Firstly set your editbox (UITextView I guess ??) to x coordinate 320 (so it will not appear).
Secondly when user hit the segmented control just translate your UITextView using UIView animation :
[UIView beginAnimation:nil context:nil];
[UIView setAnimationDuration: 1.0];
CGAffineTransform trans = CGAffineTransformMakeTranslation(-320, 0);
self.view.transform = trans;
[UIView commitAnimations];
Hope it will help you ;) .
Ok, so I will try to be more precise, I guess you are using Interface Builder ?
So you have to "link" an action to you UISegmentedController, so in your class write this method :
-(IBAction) translateMyView
{
//If the first segment is selected do translation of the cellView
if(yourSegmentedController.selectedSegmentIndex == 0)
{
[UIView beginAnimation:nil context:nil];
[UIView setAnimationDuration: 1.0];
//This will translate the view to its position from its position -320 px
CGAffineTransform trans = CGAffineTransformMakeTranslation(-320, 0);
//Replace self.view with the view you want to translate.
self.view.transform = trans;
[UIView commitAnimations];
}
else if(yourSegementedController.selectedSegmentIndex ==1)
{
//Do same thing that above but with another view
}
}
So this is the action that occure when you select an index in your segmentedController.
What you have to do is linking this action to your UISegmentedController in Interface Builder.
Hope it will be helpfull ;-)