1.建立一个view文件TouchView将他指定为一个viewcontroller的跟视图
-(void)loadView
{
self.touchView = [[TouchView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.touchView;
}
2.这个TouchView的具体代码
#import "TouchView.h"
#define kScreenwidth [UIScreen mainScreen].bounds.size.width
#define kScreenheigh [UIScreen mainScreen].bounds.size.height
@interface TouchView()
@property(nonatomic,retain)UIView*aView;
@property(nonatomic,retain)UILabel*blable;
@end
@implementation TouchView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addView];
}
return self;
}
-(void)addView
{
self.blable = [[UILabel alloc]initWithFrame:CGRectMake(0 , 0, 100, 100)];
self.blable.center = CGPointMake(kScreenwidth/2, 50);
self.blable.layer.cornerRadius = 50;
self.blable.layer.masksToBounds = YES;
self.blable.text = @"❤️";
self.blable.font = [UIFont systemFontOfSize:50];
[self addSubview:self.blable];
}
//开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"开始摸了....");
// self.aView.backgroundColor = [UIColor blueColor];
// CGFloat red = arc4random()%256/255.0;
// CGFloat green = arc4random()%256/255.0;
// CGFloat blue = arc4random()%256/255.0;
// self.aView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
//移动
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"移动了....");
/*
//aview随机变位置
CGFloat x = (arc4random()%(int)(kScreenwidth-200+1)+100)/1.0; //kScreenwidth 是double类型的 而且取余数的是整数
CGFloat y = (arc4random()%(int)(kScreenheigh-200+1)+100)/1.0;
self.aView.center = CGPointMake(x, y);
////aview随机颜色
CGFloat red = arc4random()%256/255.0;
CGFloat green = arc4random()%256/255.0;
CGFloat blue = arc4random()%256/255.0;
self.aView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
CGFloat weidth = arc4random()%(200-100+1)+100;
CGFloat heigh = arc4random()%(200-100+1)+100;
self.aView.bounds = CGRectMake(0, 0, weidth, heigh);
*/
//移动
/*
UITouch*touch = [touches anyObject];
//获取两个点
//获取上一个点
CGPoint p1 = [touch previousLocationInView:_aView];
//获取当前的点
CGPoint p2 = [touch locationInView:_aView];
//移动的效果
// CGPoint p = CGPointMake(_aView.center.x+(p2.x-p1.x), _aView.center.y+(p2.y-p1.y));
//解锁效果
CGPoint p = CGPointMake(_aView.center.x+p2.x-p1.x, _aView.center.y);
//改变的视图
_aView.center = p;
if (_aView.center.x>kScreenwidth-50) {
self.backgroundColor = [UIColor yellowColor];
}else{
self.backgroundColor = [UIColor whiteColor];
}
*/
UITouch*touch = [touches anyObject];
//获取视图上的两个点
CGPoint p1 = [touch previousLocationInView:_blable];
CGPoint p2 = [touch locationInView:_blable];
//计算偏移后的点
CGPoint p = CGPointMake(_blable.center.x, _blable.center.y+p2.y-p1.y);
//偏移中心
_blable.center = p;
if (_blable.center.y >kScreenheigh-50) {
_blable.text = @"❤️";
_blable.transform = CGAffineTransformMakeRotation(M_1_PI);
}else{
_blable.text = @"❤️";
_blable.transform = CGAffineTransformMakeRotation(0);
}
}
//结束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"摸完了...");
// self.aView.backgroundColor = [UIColor redColor];
}
//取消
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"取消了");
}
@end