How to animate the image in objective-c?
I need if he touchBegan on the image size should be increased if he moved the also same but if in touchesended it needs to be become original size how t开发者_StackOverflowo do this .can any one share the code to do this ..thanks in advance..
Let's guess that your image is implemented as UIImageView, If so, you can use simple transformation.
yourImage.transform = CGAffineTransformMakeScale(scale.x, scale.y);
scale (1.0 - original size)
I guess you subclassed UIImageView - if you did not, you should do it now. Also, make sure the set the image's .userInteractionEnabled to YES!
Interface:
@interface YourImageView : UIImageView
@property (nonatomic, assign) CGPoint originalCenter;
@property (nonatomic, assign) CGPoint touchLocation;
@end
Implamentation:
@implementation YourImageView
@synthesize originalCenter;
@synthesize touchLocation;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
self.originalCenter = self.center;
self.touchLocation = [[touches anyObject] locationInView:self.superview];
self.transform = CGAffineTransformMakeScale(1.5, 1.5);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
CGPoint touch = [[touches anyObject] locationInView:self.superview];
CGFloat xDifference = (touch.x - self.touchLocation.x);
CGFloat yDifference = (touch.y - self.touchLocation.y);
CGPoint newCenter = self.originalCenter;
newCenter.x += xDifference;
newCenter.y += yDifference;
self.center = newCenter;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
self.originalCenter = CGPointZero;
self.touchLocation = CGPointZero;
self.transform = CGAffineTransformMakeScale(1.0, 1.0);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
self.originalCenter = CGPointZero;
self.touchLocation = CGPointZero;
self.transform = CGAffineTransformMakeScale(1.0, 1.0);
}
@end
You could, if course, warp the self.transform = sth into an animation to make it look better. ;)
You can increase the frame size of the imageview and scale the image displayed in it (by using scaleimage to fit) in the touchesBegan Method. U can set the frame to orginal size in the touchesEnded method. In this way u can achieve the animation effect u desired.Hope this helps.
Place this code in your touchesBegan
[UIView animateWithDuration:0.3 animations:^{
myImage.transform = CGAffineTransformMakeScale(1.5, 1.5);
}];
Place this code in your touchesEnded
[UIView animateWithDuration:0.3 animations:^{
myImage.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
精彩评论