美文网首页
iOS 视图可移动

iOS 视图可移动

作者: 江河_ios | 来源:发表于2022-05-14 22:19 被阅读0次

    iOS 创建可移动的视图

    .h

    #import <UIKit/UIKit.h>
    @interface YLRTCVideoMiniView : UIView
    @property(nonatomic,copy)NSString *Str;
    @end
    

    .m

    #import "YLRTCVideoMiniView.h"
    
    @interface YLRTCVideoMiniView() {
    CGPoint beginpoint;
    CGFloat maxScaleW;
    CGFloat maxScaleH;
    CGFloat minScaleW;
    CGFloat minScaleH;
    }
    @end
    
    @implementation YLRTCVideoMiniView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
        minScaleW = -self.frame.origin.x;
        maxScaleW = KSCREEN_WIDTH-self.width-self.frame.origin.x;
    
        minScaleH = -self.frame.origin.y;
        maxScaleH = KSCREEN_HEIGHT-self.height-self.frame.origin.y;
        NSLog(@"======copyStr============= %@",self.Str);
    }
    return self;
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"我正在移动中");
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self];
    
    float offsetX = currentPosition.x - beginpoint.x;
    float offsetY = currentPosition.y - beginpoint.y;
    
    if (self.transform.tx >= maxScaleW) {
        offsetX = offsetX > 0 ? 0 : offsetX;
    } else if (self.transform.tx <= minScaleW) {
        offsetX = offsetX < 0 ? 0 : offsetX;
    }
    
    if (self.transform.ty >= maxScaleH) {
        offsetY = offsetY > 0 ? 0 : offsetY;
    } else if (self.transform.ty <= minScaleH) {
        offsetY = offsetY < 0 ? 0 : offsetY;
    }
    
    self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
    }
    
      - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"我开始移动了");
    UITouch *touch = [touches anyObject];
    beginpoint = [touch locationInView:self];
     }
    
      - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"我滑动结束了");
    }
    
      @end
    

    相关文章

      网友评论

          本文标题:iOS 视图可移动

          本文链接:https://www.haomeiwen.com/subject/ozjdertx.html