2013-02-25 120 views
0

我有一些圖紙上我的自定義視圖,想讓用戶將其旋轉旋轉自定義視圖?

viewContorller.m:

-(void)setMyView:(myView *)myView { 
... 
    [self.faceView addGestureRecognizer:[[UIRotationGestureRecognizer alloc] initWithTarget:faceView action:@selector(rotate:)]]; 
... 
} 

faceView.m

- (void)rotate:(UIRotationGestureRecognizer *)gesture 
{ 
    if (gesture.state == UIGestureRecognizerStateChanged) { 
     self.transform = CGAffineTransformMakeRotation(gesture.rotation); 
     gesture.rotation = 0; 
    } 
} 

它只是不工作,但相當顫抖?

回答

0

使用下面的代碼,並在viewController.h類中添加UIGestureRecognizerDelegate。

-(void)setMyView:(myView *)myView { 
//... 
     UIRotationGestureRecognizer* rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)]; 
     rotateRecognizer.delegate = self; 
     [self.faceView addGestureRecognizer:rotateRecognizer]; 

//... 
} 

我認爲這會對您有所幫助。

0

好了,問題是你的旋轉方法,試試這個代碼,

- (void)rotate:(UIRotationGestureRecognizer *)gesture 
{ 
    if (gesture.state == UIGestureRecognizerStateChanged) { 
     self.transform = CGAffineTransformRotate(self.transform, [gesture rotation]); 
     gesture.rotation = 0; 
    } 
} 
+0

試過這個,它在旋轉時縮放,如何解決? – PlusA 2013-02-26 07:07:54

0

使用此代碼,以旋轉視圖,使用戶交互和多點觸摸啓用查看。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotatePiece:)]; 
    [self.myview addGestureRecognizer:rotationGesture]; 
} 

- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer 
{ 
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer]; 

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { 
     [gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]); 
     [gestureRecognizer setRotation:0]; 
    } 
} 

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
{ 
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 
     UIView *piece = gestureRecognizer.view; 
     CGPoint locationInView = [gestureRecognizer locationInView:piece]; 
     CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview]; 

     piece.layer.anchorPoint = CGPointMake(locationInView.x/piece.bounds.size.width, locationInView.y/piece.bounds.size.height); 
     piece.center = locationInSuperview; 
    } 
} 

此代碼來自蘋果示例代碼touches

+0

它在旋轉時縮放。 – PlusA 2013-02-26 07:08:37

+0

我不明白你在說什麼,你能解釋一下嗎? – Dilip 2013-02-26 07:13:27

+0

在接近K * 90度時接近K * 45度時它變得更大,而我旋轉它時變得更小! (也在控制檯上得到很多東西) – PlusA 2013-02-26 07:18:44