IOS Animated Imageview intercepts another image - ios

So im new to objective-c and i have a image that's moving up and down the screen at all times , how would i code if another image touches or intercepts that image . this is the animation code i have done , but still confused on how to write the second part. thank you for your help
Animation:
//LINE ANAMATION
layer = line.layer;
CGPoint startPoint = (CGPoint){line.center.x,20};
CGPoint endPoint = (CGPoint){line.center.x, screenSizeY/2};
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.duration = 3.f;
animation.path = thePath;
animation.autoreverses = YES;
animation.repeatCount = INFINITY;
[layer addAnimation:animation forKey:#"position"];

Try:
if (CGRectIntersectsRect(self.imageView1.frame, self.imageView2.frame))
{
NSLog(#"Intersected!");
}
else
{
NSLog(#"No intersection :(");
}

Related

Can I rotate UIImage around the centre with animation?

I want to rotate image in circular animation. How can I do it in a simple way?
I know it is not difficult but I'm new to iOS.
Can anybody tell me right code ?
its my solution:
- (void)startAnimation
{
[UIView animateWithDuration:.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[self.loadingRingImageView setTransform:CGAffineTransformRotate(self.loadingRingImageView.transform, M_SQRT2)];
} completion:^(BOOL finished) {
if (finished)
{
[self startAnimation];
}
}];
}
[[self.<imageviewinstance> layer] addAnimation:[self rotationAnimation] forKey:nil];
Add the animation as above and the below is the animation code.
- (CABasicAnimation *)rotationAnimation
{
CABasicAnimation *rotAnim = [CABasicAnimation animationWithKeyPath:#"position"];
[rotAnim setFromValue:[NSValue valueWithCGPoint:CGPointMake(100.0, 400.0)]];
rotAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(300.0, 150.0)];
// rotAnim.duration = 5.0;
rotAnim.autoreverses = YES;
// rotAnim.repeatCount = HUGE_VAL;
rotAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return rotAnim;
}
if the above is not worked use this
- (CABasicAnimation *)rotationAnimation
{
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:#"transform.rotation" ];
move.delegate = self;
[move setFromValue:[NSNumber numberWithFloat:0]];
[move setToValue:[NSNumber numberWithFloat:50]];
[move setDuration:10.0f];
move.autoreverses = YES;
}
the answer of question i was wondering is ->
UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:imageToAnimate];
imageViewForAnimation.alpha = 1.0f;
CGRect imageFrame = imageViewForAnimation.frame;
//Your image frame.origin from where the animation need to get start
CGPoint viewOrigin = imageViewForAnimation.frame.origin;
viewOrigin.y = viewOrigin.y + imageFrame.size.height / 2.0f;
viewOrigin.x = viewOrigin.x + imageFrame.size.width / 2.0f;
imageViewForAnimation.frame = imageFrame;
imageViewForAnimation.layer.position = viewOrigin;
[self.view addSubview:imageViewForAnimation];
// Set up fade out effect
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
[fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]];
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.removedOnCompletion = NO;
// Set up scaling
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:#"bounds.size"];
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f / imageFrame.size.width))]];
resizeAnimation.fillMode = kCAFillModeForwards;
resizeAnimation.removedOnCompletion = NO;
// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
//Setting Endpoint of the animation
CGPoint endPoint = CGPointMake(480.0f - 30.0f, 40.0f);
//to end animation in last tab use
//CGPoint endPoint = CGPointMake( 320-40.0f, 480.0f);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
CAAnimationGroup *group = [CAAnimationGroup animation];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]];
group.duration = 0.7f;
group.delegate = self;
[group setValue:imageViewForAnimation forKey:#"imageViewBeingAnimated"];
[imageViewForAnimation.layer addAnimation:group forKey:#"savingAnimation"];

CAEmitterLayer - kCAEmitterLayerCircle, circular path?

I'm trying to recreate this effect (see image below), but with a circular path as I have a round button.
Here's the source.
https://github.com/uiue/ParticleButton/blob/master/ParticleButton/EmitterView.m
The modifications I've made seem to affect the method that the particle is animated, not the path.
I haven't researched the technology employed in depth, however I don't believe it's possible?
fireEmitter = (CAEmitterLayer *)self.layer;
//fireEmitter.renderMode = kCAEmitterLayerAdditive;
//fireEmitter.emitterShape = kCAEmitterLayerLine;
// CGPoint pt = [[touches anyObject] locationInView:self];
float multiplier = 0.25f;
fireEmitter.emitterPosition = self.center;
fireEmitter.emitterMode = kCAEmitterLayerOutline;
fireEmitter.emitterShape = kCAEmitterLayerCircle;
fireEmitter.renderMode = kCAEmitterLayerAdditive;
fireEmitter.emitterSize = CGSizeMake(100 * multiplier, 0);
CustomButton.m
-(void)drawRect:(CGRect)rect
{
//绘制路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.duration = 3;
animation.timingFunction = [CAMediaTimingFunction functionWithName:#"easeInEaseOut"];
animation.repeatCount = MAXFLOAT;
// animation.path = path;
animation.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath;
[emitterView.layer addAnimation:animation forKey:#"test"];
}
Create a custom button in circle shape.
CustomButton *button = [[CustomButton alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];
[self.view addSubview:button];
button.center = self.view.center;
button.layer.cornerRadius = button.frame.size.width/2.f;
button.clipsToBounds = YES;
You 're just setting the emitter properties there (irrelevant with the animation of its position). The part where the animation is created and added to the button is missing from your question (but is present in the sample project).
There in the drawRect: method the animation is added as follows:
-(void)drawRect:(CGRect)rect
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.duration = 3;
animation.timingFunction = [CAMediaTimingFunction functionWithName:#"easeInEaseOut"];
animation.repeatCount = MAXFLOAT;
animation.path = path; // That's what defines the path of the animation
[emitterView.layer addAnimation:animation forKey:#"test"];
}
Which essentially says, take this path (right now a rectangle with the dimensions of the button) and animate the emitterViews (the particles) position along this path for 3 seconds (and repeat forever).
So, a simple change in animation.path to something like this for example could define a circular path for emitter to follow:
animation.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath;
(Essentially an oval with the dimensions of the button - which might be what you want, or not - but you get the idea...)
Note: I'm not suggesting that drawRect: is the best place to create and add the animation (it's just based on the sample that you have provided). You could create and add the animation any way you like provided that you have a path matching your button's circular shape.

IOS current keyframe y axis

Hey im trying to show the y axis of my imageview while its moving , but its not displaying the animating y axis , its showing only one number .
//LINE ANAMATION
CALayer *layer = line.layer;
CGPoint startPoint = (CGPoint){line.center.x,20};
CGPoint endPoint = (CGPoint){line.center.x, screenSizeY/2};
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.duration = 3.f;
animation.path = thePath;
animation.autoreverses = YES;
animation.repeatCount = INFINITY;
[layer addAnimation:animation forKey:#"position"];
NSLog(#"%f", line.center.y);
There are a couple of things wrong with this. First, the animation state is maintained in the presentation layer. Your view and underlying layer are will reflect the final state of the animation immediately. Second, your NSLog statement will only be called once because you aren't in a loop.
If you want to do something at each frame of an animation, you can use CADisplayLink to get a callback on each frame. In this callback, you can check the current Y position by looking at the presentation layer:
- (void)myCallback:(CADisplayLink *)link
{
NSLog(#"%f", CGRectGetMidY(line.layer.presentationLayer.frame));
}

Xcode How to move an image up and down animation

Hey im trying to move an imageview down to the middle of the screen then back up to the top . here is my code , right now it just moves to the middle and stays there .
-(void)lineGoBack
{
//move diagonally down
line.center = CGPointMake(line.center.x,
line.center.y-10);
//start time for first method
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(LineMovement) userInfo:nil repeats:NO];
}
//The LINE
-(void)LineMovement
{
lineMovementOffset = CGPointMake(line.center.x, screenSizeY/2);
//move diagonally up
line.center = CGPointMake(line.center.x,
line.center.y+10);
//start time for second method
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(lineGoBack) userInfo:nil repeats:NO];
}
I don't like using blocks based animations for chained animations - they tend to grow difficult to read. I would suggest using keyframe animations. This also enables you to easily define a number of intermediary points to your animation, or add more advanced features to your animation, e.g. curves.
Here is an example of how you can animate a UIView linearly between three points.
CGPoint startPoint = (CGPoint){100.f, 100.f};
CGPoint middlePoint = (CGPoint){400.f, 400.f};
CGPoint endPoint = (CGPoint){600.f, 100.f};
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, middlePoint.x, middlePoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.duration = 3.f;
animation.path = thePath;
[self.animatedView.layer addAnimation:animation forKey:#"position"];
self.animatedView.layer.position = endPoint;
To do an infinite animation between two points, you could use the code below. Applying it to a UIImageView would work the same.
CGPoint startPoint = (CGPoint){100.f, 100.f};
CGPoint endPoint = (CGPoint){400.f, 400.f};
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.duration = 3.f;
animation.path = thePath;
animation.autoreverses = YES;
animation.repeatCount = INFINITY;
[self.animatedView.layer addAnimation:animation forKey:#"position"];
I would propose to use [UIView -animateWithDuration:animations:completion] for that:
[UIView animateWithDuration:0.5f
animations:^{
imageView.center = CGPointMake(imageView.center.x, imageView.center.y + 10.0f);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5f
animations:^{
imageView.center = CGPointMake(imageView.center.x, imageView.center.y - 10.0f);
} completion:^(BOOL finished) {
}];
}];
See Apple Documentation.
Besides that: method names shouldn't start with an uppercase letter

CAKeyframeAnimation repeat animation from begin to end

I need to add infinite animation for UIImageView from start point to end point and after that from end point to start point.
I am trying like this:
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 2.0;
pathAnimation.repeatCount = HUGE_VALF;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CGPoint endPoint = CGPointMake(320.0f - 30.0f, self.myImage.frame.size.height);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, self.myImage.frame.origin.x, self.myImage.frame.origin.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, self.myImage.frame.origin.y,
endPoint.x, self.myImage.frame.origin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
[self.myImage.layer addAnimation:pathAnimation forKey:#"curveAnimation"];
myImage moves from start point to end ok, but after that move to start point and repeat animation from start point to end. So, There is no animation from end point to start. Is it possible to do?
Animating back the same way as you came is as simple as setting autoreverses to YES.
That said, If it's truly an infinite animation then you don't need the fillMode or the removedOnCompletion
I were you, I'd simply use :
[UIVIew animateWithDuration:delay:options:animations:completion:]
with the options
UIViewAnimationOptionRepeat

Resources