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

时间:2011-07-19 13:23:27

标签: iphone objective-c cocoa-touch

我需要,如果他触摸开始图像大小应该增加,如果他移动也相同,但如果 在touchesended它需要成为原始大小如何做到这一点。任何人共享代码来做到这一点..提前谢谢..

4 个答案:

答案 0 :(得分:1)

让我们猜测您的图像是作为UIImageView实现的,如果是这样,您可以使用简单的转换。

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

比例(1.0 - 原始大小)

答案 1 :(得分:1)

我猜你是UIImageView的子类 - 如果你没有,你应该现在就做。 另外,请确保将图像的.userInteractionEnabled设置为YES!

接口:

@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

如果当然,你可以将self.transform = sth变成动画,使它看起来更好。 ;)

答案 2 :(得分:0)

您可以在touchesBegan方法中增加imageview的帧大小并缩放其中显示的图像(通过使用scaleimage来拟合)。 U可以在touchesEnded方法中将帧设置为原始大小。通过这种方式你可以实现你想要的动画效果。希望这会有所帮助。

答案 3 :(得分:0)

将此代码放在touchesBegan

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

将此代码放入touchesEnded

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