自定义组件,拖拽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
- 自定义组件,拖拽BUTTON
- Android自定义Button组件
- 自定义Button组件
- flex自定义组件button实现关闭
- React native自定义组件之Button按钮
- 圈子金融的weex领悟 -自定义button组件
- 自定义button
- 自定义BUTTON!
- 自定义button
- 自定义Button
- 自定义button
- 自定义button
- 自定义Button
- 自定义Button
- 自定义Button
- 自定义button
- 自定义checkbox,自定义button
- Button控件+自定义Button控件
- Javascript中继承的实现方法
- 提升业务效率,体现IT价值
- [Rqnoj-192]梦幻大PK
- 软件需求挖掘步骤
- .net 中异步SOCKET发送数据时碰到的内存问题
- 自定义组件,拖拽BUTTON
- 工作笔记2012-1-4号前
- hdu 1217 map+floyd
- 0年级码农的2011年终总结
- 局域网聊天软件的设计思路
- jQuery调用WCF
- Compiler knowedge -- gcc & mem management
- WebGL three.js
- hive脚本运行查看错误日志方式