uiview的UIEvent事件通过UIEvent来移动

31 篇文章 0 订阅
9 篇文章 0 订阅


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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值