我有UIView,使用
进行垂直变换currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0f)));
现在我需要通过触摸将这个UIView从一个位置移动到另一个位置,为此我已经使用了
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
但我无法维持变革,我们如何才能实现这一目标?
基本上我已经使用以下调用
在UIView上添加了一些UI元素-(void)AddItemOnView:(UIView*)aView
Angle:(CGFloat)aDegree
XOrigin:(CGFloat)aXOrigin
YOrigin:(CGFloat)aYOrigin
Width:(CGFloat)aWidth
Height:(CGFloat)aHeight
FlipX:(CGFloat)aFlipHorrizontal
FlipY:(CGFloat)aFlipVerticle
{
UIView* currentView = aView;
if(currentView)
{
CGFloat angle = aDegree;
CGFloat flipHorrizontal = aFlipHorrizontal;
CGFloat flipVerticle = aFlipVerticle;
CGFloat xOrigin = aXOrigin;
CGFloat yOrigin = aYOrigin;
CGFloat width = aWidth;
CGFloat height = aHeight;
currentView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
currentView.frame = CGRectIntegral(CGRectMake(0, 0, width, height));
/* Flip The View Horrizontly*/
if(flipHorrizontal < 0)
{
/* Concat With previous Layer Operation */
currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0f)));
/* Need to set anchor point ==> Top Right Corner */
currentView.layer.anchorPoint = CGPointMake(1.0f, 0.0f);
}
/* Flip The View Verticaly*/
if(flipVerticle < 0)
{
/* Concat With previous Layer Operation */
currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0f)));
if(flipHorrizontal < 0)
{
/* This needs to set as we have already done flip X */
/* Need to set anchor point ==> Bottom Right Corner */
currentView.layer.anchorPoint = CGPointMake(1.0f, 1.0f);
}
else
{
/* Need to set anchor point ==> Bottom Left Corner */
currentView.layer.anchorPoint = CGPointMake(0.0f, 1.0f);
}
}
/* Perform Rotation */
if(angle != 0)
{
/* Concat With previous Layer Operation */
currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(DegreesToRadians(angle), 0, 0, 1.0)));
if(flipHorrizontal < 0 || flipVerticle < 0)
{
/* Countinue with previous anchor point */
}
else
{
/* Need to set anchor point ==> Top Left Corner */
currentView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
}
}
/* Set Origins of View */
currentView.layer.position = CGPointMake(xOrigin, yOrigin);
[self addSubview:currentView];
}
}
现在我希望移动这些添加了UIViews的变换。
答案 0 :(得分:4)
我不确定你的意思是“我无法维持变革”无论如何,这是一种我觉得可能对你有所帮助的方法。
对于初学者,当您向视图应用“身份”变换以外的变换时,frame
属性变得毫无意义。这意味着您无法使用其origin
成员来更改视图的位置。您必须使用视图的center
属性。
另外,对于拖动,我强烈建议您使用UIPanGestureRecognizer
代替touches...
方法。这是因为手势识别器为您维护状态,并且可以非常容易地拖动它。
以下是一些示例代码:
// Create your view and apply all the transforms you want
// --code here--
// Create and assign the UIPanGestureRecognizer
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
[self.currentView addGestureRecognizer:panGesture];
// Here is where the dragging happens
-(void)panDetected:(UIPanGestureRecognizer*)panGesture {
// Get the view that detected the gesture
UIView *view = panGesture.view;
// If dragging started or changed...
if (panGesture.state == UIGestureRecognizerStateBegan || panGesture.state == UIGestureRecognizerStateChanged) {
// Get the translation in superview coordinates
CGPoint translation = [panGesture translationInView:view.superview];
// Get your view's center
CGPoint viewCenter = view.center;
// Add the delta
viewCenter.x += translation.x;
viewCenter.y += translation.y;
view.center = viewCenter;
// Reset delta from the gesture recognizer
[panGesture setTranslation:CGPointZero inView:view.superview];
}
}
我在我的一个项目中测试了这个代码,该视图具有旋转变换并且运行良好。
希望这有帮助!