1

我在拖动图像时遇到了一些问题。所以我使用这段代码:

-(void)panDetected:(UIPanGestureRecognizer *)panRecognizer {
    CGPoint translation = [panRecognizer translationInView:self.view];
    CGPoint imageViewPosition = player1.center;
    imageViewPosition.x +=translation.x;
    imageViewPosition.y +=translation.y;
    player1.center = imageViewPosition;
    [panRecognizer setTranslation:CGPointZero inView:self.view];
    [self SaveColorRealTime];
}

-(void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer {
    CGFloat scale = pinchRecognizer.scale;
    player1.transform = CGAffineTransformScale(player1.transform, scale, scale);
    pinchRecognizer.scale = 1.0;

}

-(void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer {
    CGFloat angle = rotationRecognizer.rotation;
    player1.transform = CGAffineTransformRotate(player1.transform, angle);
    rotationRecognizer.rotation = 0.0;
}

-(void)tapDetected:(UITapGestureRecognizer *)tapRecognizer {
    [UIView animateWithDuration:0.25 animations:^{
        player1.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
        player1.transform = CGAffineTransformIdentity;
    }];
}
- (void)viewDidLoad
{
    [super viewDidLoad];



player1.userInteractionEnabled = YES;

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    [player1 addGestureRecognizer:panRecognizer]; 

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
[player1 addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationDetected:)];
[player1 addGestureRecognizer:rotationRecognizer];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 2;
[player1 addGestureRecognizer:tapRecognizer];

它可以工作,用户可以在视图上拖动图像,但是当用户转到其他视图时,图像的位置会刷新。你有什么想法如何保存图像位置?谢谢

4

0 回答 0