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