[英]How to Move UIView using touchesMoved having affineTransform
我有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。
我不確定您所說的“我無法保持轉換”是什么意思,不過,我認為這可能會有所幫助。
對於初學者,當您將視圖應用於“ Identity”變換之外的其他變換時, frame
屬性將變得毫無意義。 這意味着您不能使用其origin
成員來更改視圖的位置。 您必須改為使用視圖的center
屬性。
另外,對於拖動,我強烈建議您使用UIPanGestureRecognizer
而不是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];
}
}
我在一個我的項目中測試了此代碼,該項目的視圖具有旋轉變換並且可以完美運行。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.