I want to rotate an image by 360 degree delay for 2 or 3 seconds again rotate it by 360 degree and the process should continue on. Any possible suggestions for the same ? Actually tried Cgaffinetransfom but its not working for 360 degree .
You can rotate your image like this:
[YourImageView setTransform:CGAffineTransformMakeRotation(10*(M_PI/360))];
Now to perform it continuous you need to set the timer like this:
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(doRotateImage) userInfo:nil repeats:YES];
Rotate function:
-(void)doRotateImage{
[YourImageView setTransform:CGAffineTransformMakeRotation(10*(M_PI/360))];
}
Thats it.
You could try this method:
- (void)rotateImageView{
[UIView animateWithDuration:0.4f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[yourImageView setTransform:CGAffineTransformRotate(yourImageView.transform, M_PI_2)];
}completion:^(BOOL finished){
if (finished) {
[self rotateImageView];
}
}];
}
Related
I want to animate the text of a UILabel so that it shows one text for a couple of seconds and after that fades into a default text.
I'm currently using the following code:
-(void)tapOnBalance:(id)sender{
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
cell.amountLabel.text = #"Hola!";
} completion:nil];
// Pause the function - act as a 'delay'
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
cell.amountLabel.text = #"Hallo!";
} completion:nil];
}
This works, but the [NSRunLoop currentRunLoop] is pausing the entire app, blocking everything for 3 seconds.
How do I get rid of the block on the main thread and still get the same result?
A simple fade (not a cross-fade) can go like this:
// change a label's text after some delay by fading
// out, changing the text, then fading back in
- (void)fadeLabel:(UILabel *)label toText:(NSString *)toText completion:(void (^)(BOOL))completion {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished) {
label.text = toText;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
label.alpha = 1.0;
} completion:completion];
}];
}
// simpler signature that can be done on a perform
- (void)changeLabelText:(NSString *)toText {
[self fadeLabel:self.myLabel toText:toText completion:nil];
}
// set the label to something, then change it with a fade, then change it back
self.myLabel.text = #"text";
[self performSelector:#selector(changeLabelText:) withObject:#"hola!" afterDelay:4];
[self performSelector:#selector(changeLabelText:) withObject:#"text" afterDelay:8];
You could also nest the calls' completion blocks (and add a delay param) to do something similar without the performs. To do a cross-fade, apply a similar technique with two labels (that have the same frame) where one's alpha is zero and the other's alpha is one.
After creating an image at a random location (spawnGood), if that object collides with the user controlled image (userToken) an NSTimer should start to work which uses the 'stick' method. This method is supposed to move spawnGood to the same position as userToken if userToken is moved (eg. sticks itself to userToken). However, I get the 'unrecognised selector sent to instance' error when the two images collide. It may either be a problem with the timer or the method, so is anyone able to suggest a solution to this? Thanks in advance :)
The code below produces the described error:
-(void)spawn{
spawn = [NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:#selector(spawnMechanism)
userInfo:nil
repeats:YES];
}
-(void)stick:(UIImageView *)spawnGood{
[UIView animateWithDuration:3.0
delay: 0
options: UIViewAnimationOptionCurveLinear
animations:^{
spawnGood.center=CGPointMake(userToken.center.x, userToken.center.y);
}
completion:^(BOOL finished){
NSLog(#"complete");
}];
}
-(void)spawnMechanism{
timeSinceLastSpawn++;
if(timeSinceLastSpawn >= 3)
{
if( arc4random() % 10 < 7 ){
UIImageView *spawnGood;
spawnGood=[[UIImageView alloc]initWithFrame:CGRectMake(arc4random() % 700, -100, 70,70)];
UIImage *image;
image=[UIImage imageNamed:#"friendly-01"];
[spawnGood setImage:image];
[array addObject:spawnGood];
[self.view addSubview:spawnGood];
NSLog(#"spawnGood spawned");
[UIView animateWithDuration:2.0
delay: 0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
CGRect frame1 =[spawnGood frame];
frame1.origin.y =frame1.origin.y+950;
[spawnGood setFrame:frame1];
}
completion:^(BOOL finished){
if(CGRectIntersectsRect(userToken.frame, spawnGood.frame)){
NSLog(#"Good Collision");
stick=[NSTimer scheduledTimerWithTimeInterval:rate
target:stick
selector:#selector(stick:)
userInfo:nil
repeats:YES];
}else{
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
CGRect frame1 =[spawnGood frame];
frame1.origin.y =frame1.origin.y+950;
[spawnGood setFrame:frame1];
}
completion:nil];
};
}
];
}
}
}
You have two errors:
the target in this line
stick=[NSTimer scheduledTimerWithTimeInterval:rate
target:stick
selector:#selector(stick:)
userInfo:nil
repeats:YES];
should be 'self', not 'stick' (which is presumably an NSTimer * class variable?)
the parameter of stick should be an NSTimer *, not an image view.
You should prettify your code and add more context.
I'm trying to set the image of an UIImageView with an alternative image.
I know I can do [imageView setImage: image]. However, is it possible to add any dissolve effect(animation) when I change image?
I have done this earlier and here is the piece of code that helps you out.This code works fine for the issue you described above.
-(void)changeImage
{
myImageView.animationImages=[NSArray arrayWithArray:self.yourImageArray];
myImageView.animationDuration=10.0;
myImageView.animationRepeatCount=0;
[myImageView startAnimating];
myImageView.transform = CGAffineTransformMakeRotation(M_PI/2);
[self.view addSubview:myImageView];
//The timers time interval is the imageViews animation duration devided by the number of images in the animationImages array. 20/5 = 4
NSTimer *timer = [NSTimer timerWithTimeInterval:2.0
target:self
selector:#selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
}
//This will Fade in and fade Out Images.
-(void)onTimer{
[UIView animateWithDuration:2.0 animations:^{
myImageView.alpha = 0.0;
}];
[UIView animateWithDuration:0.5 animations:^{
myImageView.alpha = 1.0;
}];
}
If you want cross Dissolve you can use this UIViewAnimationOptionTransitionCrossDissolve.
Hope this will help you out if not please feel free ask.
You can use UIView's transitionWithView method for achieving this.
A sample code snippet:
UIImage * newImage= [UIImage imageNamed:#"newImg.png"];
[UIView transitionWithView:self.view
duration:3.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
yourImageView.image = newImage;
} completion:nil];
You can achieve this by using CAAnimation transitions
the effect you are looking for is called kCATransitionFade
Take a look here and here for some handy tutorials as well as the class reference.
I have a UIButton in a UIViewController that I currently have "fading in" to view when the app is opened. I would like to get the UIButton to slide in from left to right instead though.
I can't figure out how to get it to slide in from left to right and my attempts have failed, even though I've got the "fade in" thing down solid.
Any help you could give me? Thanks! I can add any more code as needed-
ViewController.h
#property (weak, nonatomic) IBOutlet UIButton *buttonOne;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSTimer *timermovebutton = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(movebutton) userInfo:nil repeats:NO];
[timermovebutton fire];
}
-(void) movebuttontwo
{
buttonTwo.alpha = 0;
[UIView beginAnimations:Nil context:Nil];
[UIView setAnimationDelay:0.5];
[UIView setAnimationCurve:UIViewAnimationTransitionCurlUp];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0];
buttonTwo.alpha = 1.0;
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
}
You need to animate the buttons frame, not use an animation curve.
Since iOS 4, we've had UIView animation blocks:
// first set the UIButton frame to be outside of your view:
// we are only concerned about it's location on the X-axis so let's keep all properties the same
[buttonTwo setFrame:CGRectMake(CGRectGetMaxX(self.view.frame), CGRectGetMinY(buttonTwo.frame), CGRectGetWidth(buttonTwo.frame), CGRectGetHeight(buttonTwo.frame))];
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{
// set the new frame
[buttonTwo setFrame:CGRectMake(0, CGRectGetMinY(buttonTwo.frame), CGRectGetWidth(buttonTwo.frame), CGRectGetHeight(buttonTwo.frame))];
}
completion:^(BOOL finished){
NSLog(#"Done!");
}
];
There's a good tutorial over at Ray Wenderlich you can check out to learn more.
//Set your button off the screen to the left
yourButton.frame = CGRectMake(-yourButton.frame.size.width, yourButton.frame.origin.y, CGRectGetWidth(yourButton.frame), CGRectGetHeight(yourButton.frame));
//Create the ending frame or where you want it to end up on screen
CGRect newFrm = yourButton.frame;
newFrm.origin.x = 100;
//Animate it in
[UIView animateWithDuration:2.0f animations:^{
yourButton.frame = newFrm;
}];
I'm developing an app with a scroll view in it. It's not immediately obvious that there's more content, and there's no scroll indicator (scroll view is paged).
So, to give the user a 'hey, there's something down here...', I would like to have the scroll view do a subtle bounce - down then up - on launch. I've tried this:
- (void)viewDidLoad
....
[NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:#selector(bounceScrollView) userInfo:nil repeats:NO];
}
- (void)bounceScrollView
{
[self.verticalScrollViews[0] scrollRectToVisible:CGRectMake(0, 600, 1, 1) animated:YES];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(_unbounceScrollView) userInfo:nil repeats:NO];
}
- (void)_unbounceScrollView
{
[self.verticalScrollViews[0] scrollRectToVisible:CGRectZero animated:YES];
}
However, this code makes the view get 'stuck' at about halfway between two pages.
Any help?
Idea 1: You need to turn off paging, animate the bounce, and turn the paging back on.
Idea 2: Your second move is coming way too soon:
scheduledTimerWithTimeInterval:0.01
Experiment with longer time intervals! I would start with 0.4.
Idea 3: Instead of your bounce, why not use flashScrollIndicators? This is exactly what it is for!
I had a memory problems when using NSTimer. That's why I used another solution for scrollView preview.
[UIView animateWithDuration:1.0
delay:0.00
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_scrollView.contentOffset = CGPointMake(200,0);
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
delay:0.00
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_scrollView.contentOffset = CGPointMake(0,0);
}
completion:^(BOOL finished){
}
];
}
];