iOS set UIImage with animation - ios

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.

Related

How to hide and unhide an image in iOS?

- (IBAction)press:(id)sender {
UIImage *bomb = [UIImage imageNamed:#"bom"];
UIImageView *bom =[[UIImageView alloc] initWithImage:bomb];
[bom setFrame: CGRectMake(83,115,35,35)];
[self.view addSubview:bom];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
[bom setFrame:CGRectMake(149,47,35,35)];
}
completion:^(BOOL finished)
{
[bom setFrame:CGRectMake(134,40,60,55)];
[bom setImage:[UIImage imageNamed:#"splash"]];
hits++;
_hit.text=[NSString stringWithFormat:#"%i",hits];
}];
}
Here is my code! The object bom here handles the imageview for splash.png image and i want that image to be appeared only for 0.5 sec when each time the button is pressed
You could set up an NSTimer that calls a method which hides the image. You would have to keep a reference to the bomb.
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(hideImage) userInfo:nil repeats:NO];
-(void)hideImage {
self.bomb.hidden = YES;
}

How to repeatedly call a method (reload...) with a timer to animate a transition

I implemented corePlot in my xcode project. I'm trying to "explode" a slice of the pie chart with animation. Here is the method I'm using:
- (void)radialOffsetForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)idx
{
if (myIndex == idx) {
return 20;
}
return 0;
}
I have another method which calls [pieChart reloadRadialOffset];.
For example:
- (void)thisMethod {
[pieChart reloadRadialOffset];
}
How can I animate the reloadRadialOffsets?
I just added an example to the "Simple Pie Chart" demo in the Plot Gallery example app. I added two properties to the controller to hold the index of the selected slice and the desired offset value. Since the offset is a CGFloat it is easily animated using Core Animation or CPTAnimation.
Set the index to NSNotFound to indicate that no slice should be selected. You could also use an array or set of indices if you want to highlight more than one slice at a time.
self.offsetIndex = NSNotFound;
Trigger the animation to offset the slice:
self.offsetIndex = idx;
[CPTAnimation animate:self
property:#"sliceOffset"
from:0.0
to:35.0
duration:0.5
animationCurve:CPTAnimationCurveCubicOut
delegate:nil];
The plot datasource needs the radial offset method:
-(CGFloat)radialOffsetForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index
{
return index == self.offsetIndex ? self.sliceOffset : 0.0;
}
The sliceOffset property needs a custom setter to trigger the plot to update the offsets during the animation:
-(void)setSliceOffset:(CGFloat)newOffset
{
if ( newOffset != sliceOffset ) {
sliceOffset = newOffset;
[self.graphs[0] reloadData];
}
}
In your question is bit confusing. Your question title says "how to reaptedly call a mathod using timer" and at the end of your question it changes to "How can I animate the reloadRadialOffsets?".
For repeatedly calling a method you can use any of the following options
option 1.
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(animatedPie:)
userInfo:nil
repeats:YES];
option 2:
[self performSelector:#seletor(animatePie) withObject:nil afterDelay:1.0];
and in your method
-(void) animatePie
{
[UIView animateWithDuration:1.0 animations:^
{
[pieChart reloadRadialOffsets];
} completion:^(BOOL finished)
{
[self performSelector:#seletor(animatePie) withObject:nil afterDelay:1.0];
}];
}
Here the repeated method will be called agian with a delay once the animation is complete.
When you wanted to stop the animation call
- (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(id)arg
When timer is used it will be fired once the interval is elapsed.
For animation
You can use
[UIView animateWithDuration:1.0 animations:^
{
} completion:^(BOOL finished) {
}];
or user
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];
You can use below code. In this UIViewAnimationOptionRepeat is helpful for repeat animation what you want to achieve..
[UIView animateWithDuration:5
delay:1
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionRepeat
animations:^{
[UIView setAnimationRepeatCount:2];
[pieChart reloadRadialOffsets];
}
completion:nil];
//Still you want to call method using timer
NSTimer *animateTimer;
animateTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self
selector:#selector(thisMethod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:animateTimer forMode:NSDefaultRunLoopMode];
[animateTimer fire];
- (void)thisMethod {
[pieChart reloadRadialOffsets];
}
Hope it helps you...!

Image rotation in iOS

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

Getting Timer error

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.

Bounce UIScrollView - hint that there's more

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){
}
];
}
];

Resources