iOS版;如何缩放UIimageView(永久)然后移动它

时间:2012-03-30 08:27:05

标签: iphone sdk scale move

我在这里撞墙了。我知道如何使用“CGAffineTransformMakeTranslation”移动图像,我也知道如何使用“CGAffineTransformMakeScale”缩放图像,但对于我的生活,我似乎无法让一个Image同时执行这两个并保持这种状态。它会缩放到所需大小大约一秒钟,然后立即恢复到原始大小并移动到所需位置。我需要的是让图像变大,保持大,然后移动到一个新位置(同时永久保持其新尺寸)。

以下是我在.m文件中的内容:

-(IBAction)PushZoomButton {

[UIWindow animateWithDuration:1.5
                 animations:^{
                     JustinFrame.transform = CGAffineTransformMakeScale(2.0, 2.0);
                     JustinFrame.transform = CGAffineTransformMakeTranslation(10.0, 10.0);}];



[UIWindow commitAnimations];}

对此有任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:2)

您可以使用CGAffineTransformConcat,例如:

JustinFrame.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(2.0, 2.0), CGAffineTransformMakeTranslation(10.0, 10.0));

您可能需要将翻译调整为(5,5),因为您的比例增加了一倍

答案 1 :(得分:2)

您设置的第二个变换会覆盖第一个变换。正如路易斯所说,你需要将两个变换动作连成一个。另一种写作方式是:

CGAffineTransform transform = CGAffineTransformMakeScale(2.0, 2.0);
transform = CGAffineTransformTranslate(transform, 10, 10);
JustinFrame.transform = transform;

答案 2 :(得分:0)

您可能需要查看CoreAnimation,基本上是UIView动画在引擎盖下控制的内容。如果您设置了CAAnimation,那么您想要实现的是使用动画的fillMode属性。

下面是一些示例代码,使UIView看起来像一扇门(复制粘贴了一些代码,但也许你可以修改它并发现它很有用):

- (void) pageOpenView:(UIView *)viewToOpen duration:(NSTimeInterval)duration pageTurnDirection:(PageTurnDirection) p{
// Remove existing animations before stating new animation
[viewToOpen.layer removeAllAnimations];

// Make sure view is visible
viewToOpen.hidden = NO;

// disable the view so it’s not doing anythign while animating
viewToOpen.userInteractionEnabled = NO;

float dir = p == 0 ? -1.0f : 1.0f;  // for direction calculations

// create an animation to hold the page turning
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.removedOnCompletion = NO;
transformAnimation.duration = duration;
transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

CATransform3D startTransform = CATransform3DIdentity;

if (p == NEXT_PAGE) {
    // orig values
    startTransform.m34 = 0.001f;
}else {
    // orig values
    startTransform.m34 = -0.001f;
}

// start the animation from the current state
transformAnimation.fromValue = [NSValue valueWithCATransform3D:startTransform];
// this is the basic rotation by 90 degree along the y-axis
CATransform3D endTransform = CATransform3DMakeRotation(3.141f/2.0f,
                                                       0.0f,
                                                       dir,
                                                       0.0f);
// these values control the 3D projection outlook

if (p == NEXT_PAGE) {
    endTransform.m34 = 0.001f;
    endTransform.m14 = -0.0015f;
}else {
    endTransform.m34 = -0.001f;  
    endTransform.m14 = 0.0015f;
}


transformAnimation.toValue = [NSValue valueWithCATransform3D:endTransform];


// Create an animation group to hold the rotation
CAAnimationGroup *theGroup = [CAAnimationGroup animation];

// Set self as the delegate to receive notification when the animation finishes
theGroup.delegate = self;
theGroup.duration = duration;
// CAAnimation-objects support arbitrary Key-Value pairs, we add the UIView tag
// to identify the animation later when it finishes
[theGroup setValue:[NSNumber numberWithInt:[(BODBookPageView *)viewToOpen pageNum]] forKey:@"animateViewPageNum"];  //STEPHEN: We set the tag to the page number
[theGroup setValue:[NSNumber numberWithInt: p] forKey:@"PageTurnDirection"]; 
[theGroup setValue:[NSNumber numberWithBool:YES] forKey:@"isAnimationMidpoint"];  // i.e. is this the first half of page-turning or not?

// Here you could add other animations to the array
theGroup.animations = [NSArray arrayWithObjects:transformAnimation,  nil];
theGroup.removedOnCompletion = NO;  // THIS LINE AND THE LINE BELOW WERE CRUCIAL TO GET RID OF A VERY HARD TO FIND/FIX BUG.
theGroup.fillMode = kCAFillModeForwards;  //  THIS MEANS THE ANIMATION LAYER WILL STAY IN THE STATE THE ANIMATION ENDED IN, THEREBY PREVENTING THAT ONE FRAME FLICKER BUG.
// Add the animation group to the layer
[viewToOpen.layer addAnimation:theGroup forKey:@"flipViewOpen"];

}