自定义组件,拖拽BUTTON

来源:互联网 发布:深圳龙华行知小学网站 编辑:程序博客网 时间:2024/05/18 13:48


自定义组件,拖拽BUTTON


一、头文件


#import<UIKit/UIKit.h>


@interface DragIcon :UIView//拖动的视图

{

    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



二源文件


#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];

    }

   returnself;

}


/*

// Only override drawRect: if you 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];

    }

   returnself;

}


- (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) {//到右边

        

        [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//到左边

    {

        [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



原创粉丝点击