Custom Component, drag button

Source: Internet
Author: User
Tags bool uikit


Custom Component, drag button


First, the header file


#import <UIKit/UIKit.h>


@interface Dragicon:uiview//Drag the View

{

Cgpoint lastloction;

BOOL movechanged;

Cgpoint Touchbeginpoint;

Cgpoint Touchmovedpoint;

Cgpoint Touchendpoint;

}

@property (nonatomic) Cgpoint lastloction;

@property (nonatomic,assign) ID delegate;

@end


@interface Nqscrollbutton:uiview

{

DragIcon * icon;

}

@property (nonatomic,assign) ID delegate;


@end



Two source files


#import "NQScrollButton.h"


@implementation Nqscrollbutton


@synthesize delegate;

-(ID) initWithFrame: (CGRect) frame

{

self = [super Initwithframe:frame];

if (self) {

uiimageview* Imagebg=[[uiimageview alloc] initwithimage:[uiimageimagenamed:@ "Dragbt-bg.png"];

[SELFADDSUBVIEW:IMAGEBG];

[IMAGEBG release];

Scrollcontrol=[[uiscrollview Alloc] Initwithframe:cgrectmake (0, 0, Frame.size.width, frame.size.height)];

Icon=[[dragicon Alloc] Initwithframe:cgrectmake (0,0, Frame.size.height, Frame.size.height)];

[Selfaddsubview:icon];

Icon.backgroundcolor=[uicolorclearcolor];

icon.delegate=self;

[Iconrelease];

}

return self;

}


/*

Only override Drawrect:if perform custom drawing.

An empty implementation adversely affects performance during animation.

-(void) DrawRect: (cgrect) rect

{

Drawing Code

}

*/


-(void) Dowhat: (NSString *) dowhat

{

if (self.delegate && [self.delegaterespondstoselector: @selector (Dowhat:andscrollbutton:)]) {

[Self.delegateperformselector: @selector (Dowhat:andscrollbutton:) Withobject:[nsstringstringwithstring:dowhat] Withobject:self];

}

if ([Dowhat isequaltostring:@ "left"]) {

//

}else//right

//    {

//

//    }

}

@end



@implementation DragIcon


@synthesize Lastloction, delegate;

-(ID) initWithFrame: (CGRect) frame

{

self = [super Initwithframe:frame];

if (self) {

uiimageview* Icopg=[[uiimageviewalloc] initwithimage:[uiimageimagenamed:@ "Dragbt.png"];

Icopg.frame=cgrectmake (0,0, Frame.size.width, frame.size.height);

[SELFADDSUBVIEW:ICOPG];

[ICOPG release];

Self.backgroundcolor=[uicolorclearcolor];

}

return self;

}


-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {

Uitouch *touch = [Touchesanyobject];

Cgpoint location = [Touchlocationinview:self];

self.lastloction = location;

Movechanged =no;

}


-(void) touchesended: (Nsset *) touches withevent: (Uievent *) Event {

Cgpoint location = [[Touchesanyobject] locationinview:self];

lastloction = location;

Touchmovedpoint = Cgpointzero;

if (SELF.FRAME.ORIGIN.X>SELF.SUPERVIEW.FRAME.SIZE.WIDTH/2-SELF.FRAME.SIZE.WIDTH/2) {//to the right

[uiviewanimatewithduration:0.3animations:^{

Self.frame =cgrectmake (SELF.SUPERVIEW.FRAME.SIZE.WIDTH-SELF.FRAME.SIZE.WIDTH,SELF.FRAME.ORIGIN.Y, Self.frame.size.width,self.frame.size.height);

} completion:^ (BOOL finished) {

if (self.delegate && [self.delegaterespondstoselector: @selector (dowhat:)]) {

[Self.delegateperformselector: @selector (dowhat:) withobject:[nsstringstringwithstring:@ "right"];

}

}];

}else//to the left.

{

[uiviewanimatewithduration:0.3animations:^{

Self.frame =cgrectmake (0,self.frame.origin.y,self.frame.size.width,self.frame.size.height);

} completion:^ (BOOL finished) {

if (self.delegate && [self.delegaterespondstoselector: @selector (dowhat:)]) {

[Self.delegateperformselector: @selector (dowhat:) withobject:[nsstringstringwithstring:@ "left"];

}

}];

}

}



-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event;

{


Uitouch *t = [touches anyobject];

if ([ttapcount] = = 1) {

Cgpoint _movepoint=[t LocationInView:self.superview];

CGFloat xx = 0;

if (Cgpointequaltopoint (Touchmovedpoint,cgpointzero)) {

Touchmovedpoint = _movepoint;

Return

}else{

xx = self.frame.origin.x + _movepoint.x-touchmovedpoint.x;

}

if (xx<0) {

xx=0;

}

if (xx>self.superview.frame.size.width-self.frame.size.width) {

Xx=self.superview.frame.size.width-self.frame.size.width;

}

Self.frame =cgrectmake (xx,self.frame.origin.y,self.frame.size.width,self.frame.size.height);

Touchmovedpoint = _movepoint;


}

}




@end

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.