Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 旋转UIImageView时,图像会离开屏幕/离开视图_Iphone_Ios_Objective C - Fatal编程技术网

Iphone 旋转UIImageView时,图像会离开屏幕/离开视图

Iphone 旋转UIImageView时,图像会离开屏幕/离开视图,iphone,ios,objective-c,Iphone,Ios,Objective C,我在屏幕上有一个视图,里面有一个UIImageView。用户可以平移、缩放和旋转 我有一个旋转手势设置,除了旋转图像离开屏幕/离开视图外,旋转一切都很好 知道是什么导致这种行为吗?我要寻找的预期行为是,旋转图像时,它根本不移动(旋转除外) 我已经尝试设置父视图层的锚定点,但似乎没有任何帮助 编辑:我已经在旋转手势上做了一些记录,现在发生的是图像视图中心(X)正在离开,因此它离开了屏幕 为什么旋转时坐标会改变?Y保持不变。我甚至试过关闭其他手势,旋转手势肯定有点古怪 代码如下: - (void)v

我在屏幕上有一个视图,里面有一个UIImageView。用户可以平移、缩放和旋转

我有一个旋转手势设置,除了旋转图像离开屏幕/离开视图外,旋转一切都很好

知道是什么导致这种行为吗?我要寻找的预期行为是,旋转图像时,它根本不移动(旋转除外)

我已经尝试设置父视图层的锚定点,但似乎没有任何帮助

编辑:我已经在旋转手势上做了一些记录,现在发生的是图像视图中心(X)正在离开,因此它离开了屏幕

为什么旋转时坐标会改变?Y保持不变。我甚至试过关闭其他手势,旋转手势肯定有点古怪

代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CALayer *l = [self.viewCase layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:30.0];

    self.imgUserPhoto.userInteractionEnabled = YES;
    [self.imgUserPhoto setClipsToBounds:NO];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    panRecognizer.maximumNumberOfTouches = 1;
    [self.view addGestureRecognizer:panRecognizer];

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

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

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

    panRecognizer.delegate = self;
    pinchRecognizer.delegate = self;
    rotationRecognizer.delegate = self;
}

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
    CGPoint translation = [panRecognizer translationInView:self.view];
    CGPoint imageViewPosition = self.imageView.center;
    imageViewPosition.x += translation.x;
    imageViewPosition.y += translation.y;

    self.imageView.center = imageViewPosition;
    [panRecognizer setTranslation:CGPointZero inView:self.view];
}

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

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

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

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

可能是因为平移手势也会被调用:尝试在
UIPangestureRecognitor

上将
MaximumNumberOfTouchs
设置为1如果您使用仿射变换,则设置定位点应该会起作用。你可以发布一些代码吗..我应该如何设置锚点?你可以通过设置视图层(
view.layer
)的
anchorPoint
属性来设置锚点。anchorPoint默认值为(0.5,0.5)-这是视图中心。他似乎没有搞乱任何CALayer的财产,所以主持人应该仍然坐在vie的中心。我已经添加了完整的代码。因为我在一台不同的机器上,所以我不能更早地完成。这是我的第一个想法,但将MaximumNumberOfTouchs设置为1并没有改变任何事情。