• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 14:05:18 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

引用下图。

enter image description here

捏缩放代码

   -(void)addStickersWithViewUIView*)view imageUIImage*)image{

    CGPoint center = self.imgPhoto.center;
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self actionselector(rotatePiece];
    [imgView setContentMode:UIViewContentModeScaleToFill];
    UIView *viewZoom = [[UIView alloc] initWithFrame:CGRectMake(center.x-45,center.y-45, 90, 90)];
   // [viewZoom setBackgroundColor:[UIColor redColor]];
    imgView.frame = CGRectMake(5, 5, CGRectGetWidth(viewZoom.frame)-10, CGRectGetHeight(viewZoom.frame)-10);
    [viewZoom addSubview:imgView];
    [viewZoom addGestureRecognizer:rotationGesture];

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self actionselector(scalePiece];
    [pinchGesture setDelegate:self];
    [viewZoom addGestureRecognizer:pinchGesture];

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self actionselector(moveImage];
    [panGesture setMinimumNumberOfTouches:1];
    [panGesture setMaximumNumberOfTouches:1];
    [viewZoom addGestureRecognizer:panGesture];

    UIButton *btnCloseSticker = [UIButton buttonWithType:UIButtonTypeCustom];

    [btnCloseSticker setBounds:CGRectMake(0, 0, 30,30)];
    [btnCloseSticker setImage:[UIImage imageNamed"close1.png"] forState:UIControlStateNormal];
    [btnCloseSticker addTarget:self actionselector(buttonPressed forControlEvents:UIControlEventTouchUpInside];
    self.stickerCount++;
    btnCloseSticker.tag = self.stickerCount;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self actionselector(TapToShowClose];
    tapGesture.numberOfTapsRequired = 2;
    [viewZoom addGestureRecognizer:tapGesture];
    viewZoom.layer.borderColor = [UIColor whiteColor].CGColor;
    viewZoom.layer.borderWidth = 3.0;
    viewZoom.tag = self.stickerCount+kTagBorder;
    [viewZoom addSubview:btnCloseSticker];

    [view addSubview:viewZoom];
}

-(void)hideShowBorderCloseButton{
    int borderCount = self.stickerCount-kTagBorder;
    for(int i=1;i<=borderCount;i++){
        UIView *view = [self.viewStickers viewWithTag:i+kTagBorder];
        UIView *view1 = [self.viewStickers viewWithTag:i+(kTagBorder*2)];
        view.hidden = YES;
        view1.layer.borderWidth = 0.0;
    }
}

-(void)buttonPressedid)sender{
    UIView *view = [sender superview];
    [view removeFromSuperview];
    self.stickerCount--;
}

- (void)scalePieceUIPinchGestureRecognizer *)gestureRecognizer {
    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        // Reset the last scale, necessary if there are multiple objects with different scales
        lastScale = [gestureRecognizer scale];
    }
    UIButton *btn = [self.viewStickers viewWithTag:gestureRecognizer.view.tag-kTagBorder];
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
        [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

        CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath"transform.scale"] floatValue];

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

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

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

-(void)TapToShowCloseUITapGestureRecognizer *)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 = [self.viewStickers viewWithTag:view.tag-kTagBorder];
    [(UIButton*)view1 setImage:[UIImage imageNamed"close1.png"] forState:UIControlStateNormal];
    [view1 setBounds:CGRectMake(0, 0, 30,30)];

    view1.hidden = NO;
}

- (void)moveImageUIPanGestureRecognizer *)recognizer
{
    CGPoint newCenter = [recognizer translationInView:self.view];
    [self hideShowBorderCloseButton];
 if([recognizer state] == UIGestureRecognizerStateBegan) {
        beginX = recognizer.view.center.x;
        beginY = recognizer.view.center.y;
    }
    newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);

        [recognizer.view setCenter:newCenter];

}


- (BOOL)gestureRecognizerUIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizerUIGestureRecognizer *)otherGestureRecognizer {
    // if the gesture recognizers are on different views, don't allow simultaneous recognition
    if (gestureRecognizer.view != otherGestureRecognizer.view)
        return NO;

    // if either of the gesture recognizers is the long press, don't allow simultaneous recognition
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
        return NO;

    return YES;
}

- (void)rotatePieceUIRotationGestureRecognizer *)gestureRecognizer {
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
    [self hideShowBorderCloseButton];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
        [gestureRecognizer setRotation:0];
    }
}
- (void)adjustAnchorPointForGestureRecognizerUIGestureRecognizer *)gestureRecognizer {
    [self hideShowBorderCloseButton];

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



Best Answer-推荐答案


代替缩放 View 。用缩放级别计算框架很容易。并更新 View 的框架。看看下面的方法。

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

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
        [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

        CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]);
        [self changeScale:newScale];

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

-(void)changeScale :(float)newScale{

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

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

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


}

关于ios - 缩放 super View 时如何防止按钮调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35267627/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap