Custom Implementation Moveable button

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.