The first step is to create a UIView class named Moveview
#import "MoveView.h" @implementation moveview/** move event */-(void) touchesmoved: (Nsset<uitouch *> *) touches Withevent: (uievent *) event{ Uitouch * touch = [touches anyobject]; Cgpoint Curp=[touch locationinview:self]; Cgpoint Prep=[touch previouslocationinview:self]; Self.transform = Cgaffinetransformtranslate (Self.transform, curp.x-prep.x, curp.y-prep.y);} @end
The code inside the Viewcontroller has a comment
#import "ViewController.h" #import "MoveView.h" @interface Viewcontroller ()//define a custom Uiview@property (nonatomic, weak) Moveview * Uview; @end @implementation viewcontroller-(void) viewdidload { [super viewdidload]; Create a UIView moveview * MView = [[Moveview alloc]initwithframe:cgrectmake (+, +, +)]; Set background color Mview.backgroundcolor = [self randomcolor]; Create a timer [Nstimer scheduledtimerwithtimeinterval:0.5 target:self selector: @selector (click) userinfo:nil Repeats:yes]; Self. Uview = MView; Adding Controls [Self.view Addsubview:mview];} /** Redraw line */-(void) click{self . Uview.backgroundcolor = [self randomcolor];} /** production of random colors */-(uicolor *) randomcolor{ return [Uicolor colorwithred:arc4random_uniform ()/255.0 Green: Arc4random_uniform (/255.0 blue:arc4random_uniform)/255.0 alpha:1];} @end
Xcode-Touch Move