0

我的自定义视图上有一些图纸,想让用户旋转它

视图控制器.m:

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

人脸视图.m

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

它只是不起作用,但很震动?

4

3 回答 3

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;
    }
}

此代码来自 apples 示例代码touches

于 2013-02-25T14:08:36.720 回答
0

好的,问题出在您的旋转方法上,试试这段代码,

- (void)rotate:(UIRotationGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateChanged) {
        self.transform = CGAffineTransformRotate(self.transform, [gesture rotation]);
        gesture.rotation = 0;
    }
}
于 2013-02-25T13:58:17.230 回答
0

使用以下代码并在 viewController.h 类中添加 UIGestureRecognizerDelegate。

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

//...
}

我想这会对你有所帮助。

于 2013-02-25T13:50:51.710 回答