iOS 视图可移动

iOS 创建可移动的视图

.h

#import 
@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 视图可移动)