Implemented by:
Custom Mvbutton, inherited from UIButton.
The properties are declared as follows:
@property (nonatomic) cgpoint beginpoint; @property (nonatomic) BOOL dragenable;
Customizing a button to respond to touch events
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{ //dragenable = no does not respond to action if (!_dragenable) { C3/>return; } Uitouch *touch = [touches anyobject]; Gets the current position of the button _beginpoint = [Touch locationinview:self];}
Responding when you drag a custom button
-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{ if (!_dragenable) { return; } Uitouch *touch = [touches anyobject]; Gets the target location to move to cgpoint nowpoint = [Touch locationinview:self];
Determine the offset of the target position and start position float OffsetX = nowpoint.x-_beginpoint.x; float OffsetY = nowpoint.y-_beginpoint.x; Move button Self.center = cgpointmake (self.center.x + offsetX, Self.center.y + OffsetY) According to the Center property of the button;}
The code implemented in VIEWCONTROLLER.M is as follows:
First import the custom button:
#import "MVButton.h"
Specific implementation:
-(void) viewdidload{ [Super Viewdidload]; Mvbutton *mvbutton = [[Mvbutton alloc] Initwithframe:cgrectmake (+, +, +)]; Mvbutton.backgroundcolor = [Uicolor yellowcolor]; mvbutton.dragenable = YES; [Self.view Addsubview:mvbutton];}
Custom Implementation Moveable button