gpt4 book ai didi

objective-c - 旋转后移动图像的行为不正确

转载 作者:行者123 更新时间:2023-11-29 13:48:19 24 4
gpt4 key购买 nike

我在旋转同一图像后尝试移动时遇到问题。当我尝试在旋转图像之前移动图像时,我的代码工作正常。旋转图像后,当我尝试移动旋转后的图像时,图像并没有向我拖动的方向移动。谁能帮我。提前致谢。我正在使用代码

#import "myDraggableImage.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/CoreAnimation.h>



@implementation myDraggableImage



- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{




[[self superview] bringSubviewToFront:self];
CGPoint pt = [[touches anyObject] locationInView:self];
startLocation = pt;

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[self addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[self addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self addGestureRecognizer:panRecognizer];
[panRecognizer release];

CGRect frame = [self frame];
printf("\n the x co ordinte of the frame is :%f",frame.origin.x);
printf("\n the y co ordinte of the frame is :%f",frame.origin.y);
printf("\n the widht co ordinte of the frame is :%f",frame.size.width);
printf("\n the height co ordinte of the frame is :%f",frame.size.height);


}


-(void)rotate:(id)sender
{

if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{

lastRotation = 0.0;
return;
}

CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
CGRect frame = [self frame];



}


-(void)scale:(id)sender
{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{

lastScale = 1.0;
return;
}

CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

-(void)move:(id)sender

{

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)

{
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}

translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

[[sender view] setCenter:translatedPoint];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{

CGFloat finalX = translatedPoint.x ;//+ (.15*[(UIPanGestureRecognizer*)sender velocityInView:self].x);
CGFloat finalY = translatedPoint.y ;//+ (.15*[(UIPanGestureRecognizer*)sender velocityInView:self].y);


if(finalX < 0)
{

finalX = 0;
}

else if(finalX > 460)
{

finalX = 440;
}

if(finalY < 40)
{

finalY = 40;

}

else if(finalY > 300)
{
finalY = 300;
}


[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}




- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{

return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}


-(void)dealloc
{

[super dealloc];
}

@end

最佳答案

touchesBegan:withEvent: 是设置手势的错误位置,因为它会在每次用户时添加新手势。至于翻译功能,很难说出哪里出了问题。这里的一些过程似乎有些过头了。虽然我还没有实现动画部分(引入它,对吧?),请查看 this sample project .它可以帮助您朝着正确的方向开始。

关于objective-c - 旋转后移动图像的行为不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6248513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com