简体   繁体   English

如何为 objective-c 中的图像设置动画?

[英]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 to do this.can any one share the code to do this..thanks in advance..我需要如果他 touchBegan 上的图像尺寸应该增加,如果他移动也一样但是如果触摸它需要变成原始尺寸如何做到这一点。任何人都可以分享代码来做到这一点..提前谢谢..

Let's guess that your image is implemented as UIImageView, If so, you can use simple transformation.假设您的图像实现为 UIImageView,如果是这样,您可以使用简单的转换。

yourImage.transform = CGAffineTransformMakeScale(scale.x, scale.y); yourImage.transform = CGAffineTransformMakeScale(scale.x, scale.y);

scale (1.0 - original size)比例(1.0 - 原始大小)

I guess you subclassed UIImageView - if you did not, you should do it now.我猜你是 UIImageView 的子类 - 如果你没有,你现在应该这样做。 Also, make sure the set the image's.userInteractionEnabled to YES!另外,确保将图像的.userInteractionEnabled 设置为 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.当然,您可以将 self.transform = sth 变形为 animation 以使其看起来更好。 ;) ;)

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.您可以在 touchesBegan 方法中增加 imageview 的帧大小并缩放其中显示的图像(通过使用 scaleimage 来适应)。 U can set the frame to orginal size in the touchesEnded method.您可以在 touchesEnded 方法中将框架设置为原始大小。 In this way u can achieve the animation effect u desired.Hope this helps.通过这种方式,您可以实现您想要的 animation 效果。希望这会有所帮助。

Place this code in your touchesBegan将此代码放在您的 touchesBegan 中

[UIView animateWithDuration:0.3 animations:^{
    myImage.transform = CGAffineTransformMakeScale(1.5, 1.5);
}];

Place this code in your touchesEnded将此代码放入您的 touchesEnded

[UIView animateWithDuration:0.3 animations:^{
    myImage.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM