简体   繁体   English

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

[英]iOS; How to scale UIimageView (permanently) and then move it

I've hit a wall here. 我在这里撞墙了。 I know how to move an Image using "CGAffineTransformMakeTranslation" and I also know how to scale an image using"CGAffineTransformMakeScale" but for the life of me, I can't seem to get one Image to do both of these and stay that way. 我知道如何使用“ CGAffineTransformMakeTranslation”移动图像,并且我也知道如何使用“ CGAffineTransformMakeScale”缩放图像,但是对于我来说,我似乎无法让一个图像同时完成这两个任务并保持这种状态。 It scales to the desired size for about a split second and then immediately reverts to its original size and moves to the desired location. 它会缩放到所需大小(大约一秒钟),然后立即恢复为其原始大小并移至所需位置。 What I need is for the image to get big, STAY big, and then move to a new location (while permanently staying its new size). 我需要使图像变大,保持较大,然后再移动到新位置(同时永久保持其新大小)。

Here is what I've got going on in my .m file: 这是我在.m文件中执行的操作:

-(IBAction)PushZoomButton {

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



[UIWindow commitAnimations];}

Any help with this would be appreciated! 任何帮助,将不胜感激!

you can use CGAffineTransformConcat, for instance: 您可以使用CGAffineTransformConcat,例如:

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

You may need to adapt the translation to (5, 5) since you have doubled the scale 由于比例已加倍,因此您可能需要将平移调整为(5,5)

The second transform you set overrides the first one. 您设置的第二个转换将覆盖第一个。 You need to concat both transform actions into one, as Luis said. 正如Luis所说,您需要同时将两种转换动作合为一体。 Another way of writing that would be: 另一种写法是:

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

You may need to look into CoreAnimation, basically what UIView animation is controlling under the hood. 您可能需要研究CoreAnimation,基本上是UIView动画在后台进行控制。 If you set up a CAAnimation, then what you want to achieve is done with the fillMode property of the animation. 如果设置了CAAnimation,则要使用动画的fillMode属性来实现。

Here's some example code to make a UIView look like it's opening like a door (copy pasted some code I have, but perhaps you could modify it and find it useful): 这是一些使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"];

} }

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

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