Rotating button when downloading data from server - iphone

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];

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.
}

How to know the current position of a UIImageView moving?

I have to do a little game where 2 images are moving horizontally on the same line. I want to know when they are superposed, in order to display a new image instead of 2.
What is the best way to do it ?
Way I was thinking to do :
I need to know the position somewhat during the animation of my UIImageViews and I'll try with timer to refresh the imageView when I discover that the 2 imageViews are close.
I have this function so far to move an Image :
- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve x:(CGFloat)x y:(CGFloat)y
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
Thank you for you help !
I think checking the position multiple times every second is not the best way to do it. If you can, you should precalculate the point in time, when the view overlap!
In any way, you shouldn't use the affinetransform for moving the imageview but just moving the frame of the imageview, by changing the frame or the center property of the view!
If you cant precalculate this, here are some tips on how to implement the timer and checking:
For implementing the timer you can use:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(checkPosition:) userInfo:nil repeats:YES];
This will call the function every 0.1 seconds.
- (void)checkPosition:(NSTimer *)timer
In this method you could access the imageViews UILayer to find out the actual position of this view by calling
CGFrame currentFrame = [imageView.layer presentationLayer].frame;
To be able to do this you have to import the QuartzCore framework!

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.

Animate the enabling of a UIBarButtonItem?

Is there a way to animate enabling or disabling a button? I've tried the following with no success. I'm guessing at this point that the enabled property cannot be animated like opacity can – but I hope I'm wrong.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
theButton.enabled = YES;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
I can't believe there isn't a setEnabled:(BOOL)enabled animated:(BOOL)animated method.
This is actually really easy. UIView.transitionWithView can take a snapshot of any view and cross-dissolve from the snapshot to the new state of the view. You can use that capability to animate a lot of changes, including enabled.
Just use the View Controller's view as the target view, specify TransitionCrossDissolve, then animate as you would normally.
UIView.transitionWithView(self.view,
duration: 0.5,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: { () -> Void in
self.someButton.enabled = !self.someButton.enabled
}, completion:nil)
Animatable properties (through UIView beginAnimations:context:) are: frame, bounds, center, transform, alpha (from Apple docs).
So you've to determine what exactly your animation is (in terms of color/alpha/transform animations) and create manual animation (maybe you'll have to use CAAnimation instead of UIView beginAnimations:context:)
You can break down the enable/disable of a button into a change in opacity between two overlapping images each in one state or the other (i.e. make it look like a fade-in/fade-out). In the animation completion handler you can to do the actual enable/disable toggle.
Here's an idea :)
Within your animation block, Remove the button from view and add it again with disabled state. You can specify what kinda animation you want here..
Set: self.buttonSetEvent.customView.alpha = 0.2;
and try this
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.15];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];
self.buttonSetEvent.customView.alpha = 1;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

Scale/Rotate a button in interface

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];