菜鸟教程小白 发表于 2022-12-13 14:05:18

ios - 缩放 super View 时如何防止按钮调整大小?


                                            <p><p>我在一个 View 中添加了多个手势,该 View 在 View 的角落有一个关闭按钮,一切正常,但是当我缩放时,该 View 关闭按钮也随着该 View 缩放,现在我想缩放只有那个 View 不是那个关闭按钮,请建议我怎么做?</p>

<p>引用下图。</p>

<p> <a href="/image/cbW85.png" rel="noreferrer noopener nofollow"><img src="/image/cbW85.png" alt="enter image description here"/></a> </p>

<p>捏缩放代码</p>

<pre><code>   -(void)addStickersWithView:(UIView*)view image:(UIImage*)image{

    CGPoint center = self.imgPhoto.center;
    UIImageView *imgView = [ initWithImage:image];
    UIRotationGestureRecognizer *rotationGesture = [ initWithTarget:self action:@selector(rotatePiece:)];
    ;
    UIView *viewZoom = [ initWithFrame:CGRectMake(center.x-45,center.y-45, 90, 90)];
   // ];
    imgView.frame = CGRectMake(5, 5, CGRectGetWidth(viewZoom.frame)-10, CGRectGetHeight(viewZoom.frame)-10);
    ;
    ;

    UIPinchGestureRecognizer *pinchGesture = [ initWithTarget:self action:@selector(scalePiece:)];
    ;
    ;

    UIPanGestureRecognizer *panGesture = [ initWithTarget:self action:@selector(moveImage:)];
    ;
    ;
    ;

    UIButton *btnCloseSticker = ;

    ;
    forState:UIControlStateNormal];
    ;
    self.stickerCount++;
    btnCloseSticker.tag = self.stickerCount;
    UITapGestureRecognizer *tapGesture = [initWithTarget:self action:@selector(TapToShowClose:)];
    tapGesture.numberOfTapsRequired = 2;
    ;
    viewZoom.layer.borderColor = .CGColor;
    viewZoom.layer.borderWidth = 3.0;
    viewZoom.tag = self.stickerCount+kTagBorder;
    ;

    ;
}

-(void)hideShowBorderCloseButton{
    int borderCount = self.stickerCount-kTagBorder;
    for(int i=1;i&lt;=borderCount;i++){
      UIView *view = ;
      UIView *view1 = ;
      view.hidden = YES;
      view1.layer.borderWidth = 0.0;
    }
}

-(void)buttonPressed:(id)sender{
    UIView *view = ;
    ;
    self.stickerCount--;
}

- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer {
    if( == UIGestureRecognizerStateBegan) {
      // Reset the last scale, necessary if there are multiple objects with different scales
      lastScale = ;
    }
    UIButton *btn = ;
    if ( == UIGestureRecognizerStateBegan ||
       == UIGestureRecognizerStateChanged) {

      CGFloat currentScale = [[.layer valueForKeyPath:@&#34;transform.scale&#34;] floatValue];

      // Constants to adjust the max/min values of zoom
      const CGFloat kMaxScale = 3.0;
      const CGFloat kMinScale = 1.0;

      CGFloat newScale = 1 -(lastScale - );
      newScale = MIN(newScale, kMaxScale / currentScale);
      newScale = MAX(newScale, kMinScale / currentScale);
      CGAffineTransform transform = CGAffineTransformScale([ transform], newScale, newScale);
      .transform = transform;

      lastScale = ;// Store the previous scale factor for the next pinch gesture call
    }
}

-(void)TapToShowClose:(UITapGestureRecognizer *)gestureRecognizer{
    //int borderCount = self.stickerCount-kTagBorder;

    UIView *view = gestureRecognizer.view; //cast pointer to the derived class if needed
    view.layer.borderWidth = 2.0;
    UIView *view1 = ;
    [(UIButton*)view1 setImage: forState:UIControlStateNormal];
    ;

    view1.hidden = NO;
}

- (void)moveImage:(UIPanGestureRecognizer *)recognizer
{
    CGPoint newCenter = ;
    ;
if( == UIGestureRecognizerStateBegan) {
      beginX = recognizer.view.center.x;
      beginY = recognizer.view.center.y;
    }
    newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);

      ;

}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    // if the gesture recognizers are on different views, don&#39;t allow simultaneous recognition
    if (gestureRecognizer.view != otherGestureRecognizer.view)
      return NO;

    // if either of the gesture recognizers is the long press, don&#39;t allow simultaneous recognition
    if (] || ])
      return NO;

    return YES;
}

- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer {
    ;
    ;

    if ( == UIGestureRecognizerStateBegan || == UIGestureRecognizerStateChanged) {
      .transform = CGAffineTransformRotate([ transform], );
      ;
    }
}
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    ;

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
      UIView *piece = gestureRecognizer.view;
      CGPoint locationInView = ;
      CGPoint locationInSuperview = ;

      piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
      piece.center = locationInSuperview;
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>代替缩放 View 。用缩放级别计算框架很容易。并更新 View 的框架。看看下面的方法。 </p>

<pre><code>- (IBAction)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer {
    if( == UIGestureRecognizerStateBegan) {
      // Reset the last scale, necessary if there are multiple objects with different scales
      lastScale = ;
    }

    if ( == UIGestureRecognizerStateBegan ||
       == UIGestureRecognizerStateChanged) {

      CGFloat newScale = 1 -(lastScale - );
      ;

      lastScale = ;// Store the previous scale factor for the next pinch gesture call
    }
}

-(void)changeScale :(float)newScale{

    CGAffineTransform transform = CGAffineTransformScale(, newScale, newScale);
    viewForpinch.transform = transform;

    float scale = viewForpinch.transform.a;
    float buttonScale = 1 / scale;

    btnl.transform= CGAffineTransformScale(CGAffineTransformIdentity, buttonScale, buttonScale);


}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 缩放 superView 时如何防止按钮调整大小?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35267627/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35267627/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 缩放 super View 时如何防止按钮调整大小?