2012-03-30 64 views
2

我在這裏打了一堵牆。我知道如何使用「CGAffineTransformMakeTranslation」來移動圖像,並且我也知道如何使用「CGAffineTransformMakeScale」來縮放圖像,但對於我的生活,我似乎無法讓一個Image執行這兩個操作並保持這種狀態。它在一秒鐘內縮放到所需的尺寸,然後立即恢復到原始尺寸並移動到所需的位置。我需要的是讓圖像變大,保持大度,然後移動到新的位置(同時永久保持其新尺寸)。iOS;如何縮放UIimageView(永久),然後移動它

以下是我在我的.m文件竟能上:

-(IBAction)PushZoomButton { 

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



[UIWindow commitAnimations];} 

任何幫助,將不勝感激!

回答

2

你可以使用CGAffineTransformConcat,例如:

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

您可能需要適應的翻譯(5,5),因爲你有倍增的規模

+0

這工作!謝謝! – user1299230 2012-04-04 01:04:51

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"]; 

}

2

您設置的第二個轉換覆蓋第一個轉換。正如路易斯所說,你需要將兩種變換操作連接起來。另一種寫作方式是:

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