iOS-UI-Touch移动

来源:互联网 发布:淘宝怎么发布代销 编辑:程序博客网 时间:2024/06/07 20:03
View(继承于UIView,便于引用)
Controller#import "ViewController.h"#import "MoveView.h"@interface ViewController ()@property (nonatomic, retain) MoveView *mv;@end@implementation ViewController#pragma mark - 知识点1 重写Touch方法, 实现视图- (void)creatCustomView{    self.mv = [[MoveView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];    self.mv.layer.cornerRadius = 50.;    self.mv.layer.masksToBounds = YES;     CGFloat e = arc4random()%(255 - 26 + 1) + 26;    CGFloat eNum = e / 255;    CGFloat f = arc4random()%(255 - 26 + 1) + 26;    CGFloat fNum = f / 255;    CGFloat g = arc4random()%(255 - 26 + 1) + 26;    CGFloat gNum = g / 255;    self.mv.backgroundColor = [UIColor colorWithRed:eNum green:fNum blue:gNum alpha:1];    [self.view addSubview:self.mv];    [self.mv release];}/* controll 重写touch move方法*/- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    NSLog(@"%@",touches);    /* 取出集合中的对象 */    UITouch *touch = [touches anyObject];    //UITouch *touch = [[touches anyObject] class];   // NSLog(@"%@",[[touches anyObject] class]);    /* 获取手指在view上的位置*/    CGPoint newPoint = [touch locationInView:self.view];    if (touch.view == self.mv) {        /* 更改自定义view的center属性 */    self.mv.center = newPoint;    }    CGFloat e = arc4random()%(255 - 26 + 1) + 26;    CGFloat eNum = e / 255;    CGFloat f = arc4random()%(255 - 26 + 1) + 26;    CGFloat fNum = f / 255;    CGFloat g = arc4random()%(255 - 26 + 1) + 26;    CGFloat gNum = g / 255;    self.mv.backgroundColor = [UIColor colorWithRed:eNum green:fNum blue:gNum alpha:1];}- (void)dealloc{    [_mv release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    [self creatCustomView];    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
0 0