登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

通过手势模拟了一个slider,进行颜色拾取

来自: ukop0227 分类: iOS精品源码 上传时间: 2016-5-20 09:55:28
Tag:手势 UIImage slider 滑杆

项目介绍:

通过手势模拟了一个slider,进行颜色拾取,当slider滑动到不同颜色的时候,拾取出当前选中的颜色

主要代码:
[Objective-C] 查看源文件 复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
   
    NSArray *magicFanArr =[NSArray arrayWithObjects:@"灯光",@"风扇", nil];
    UISegmentedControl *titleSegmentedControl=[[UISegmentedControl alloc] initWithItems:magicFanArr];
    titleSegmentedControl.frame =CGRectMake(0.0, 0.0, 80, 30.0);
    titleSegmentedControl.selectedSegmentIndex = 0;
    [titleSegmentedControl addTarget:self action:@selector(segmentChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.navigationItem setTitleView:titleSegmentedControl];
    
    
    
    view1 =[[UIView alloc] initWithFrame:self.view.frame];
    view1.backgroundColor =[UIColor grayColor];
    
    [self.view addSubview:view1];
    
    
    
    //**********
    UIView *pictView =[[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
    
    img =[UIImage imageNamed:@"pickColor"];
    UIImageView *pickImgView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 280, 20)];
    pickImgView.image =img;
    [pictView addSubview:pickImgView];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 40)];
    imgView.image = [UIImage imageNamed:@"slider"];
    imgView.userInteractionEnabled = YES;
    //创建手势
    UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [imgView addGestureRecognizer:pgr];
    
    
    [pictView addSubview:imgView];
    
    [view1 addSubview:pictView];
    
    
    colorView =[[UIView alloc] initWithFrame:CGRectMake(100, 200, 50, 50)];
    colorView.backgroundColor =[UIColor redColor];
    
    [view1 addSubview:colorView];
    
    
    view2 =[[UIView alloc] initWithFrame:self.view.frame];
    view2.backgroundColor =[UIColor greenColor];
}

-(void)pan:(UIPanGestureRecognizer *)pgr
{
    NSLog(@"拖动了图片");

    CGPoint point = [pgr translationInView:self.view];
    NSLog(@"--%@",NSStringFromCGPoint(point));
    static CGPoint center;
    if (pgr.state ==UIGestureRecognizerStateBegan) {
        
        center = pgr.view.center;
    }
    // pgr.view获取对应的view   view.center获取推动后的view的中心点
    pgr.view.center = CGPointMake(center.x+point.x,20);
    
    if (pgr.view.center.x < 0) {
        pgr.view.center = CGPointMake(0,20);
    }
    if (pgr.view.center.x > 280 ) {
        pgr.view.center = CGPointMake(280,20);
    }
    
    
    NSLog(@"center.x == %f",pgr.view.center.x);
    
    colorView.backgroundColor = [img colorAtPixel:CGPointMake(pgr.view.center.x+10, 10)];
    
}

相关源码推荐:

我来说两句
*滑动验证:
所有评论(4)
ukop0227 2016-5-20 09:58:54
这是代码
回复
ForApps 2016-6-12 17:35:55
666楼主v5
回复
deepindo 2016-12-15 10:23:27
强烈支持楼主ing……
回复
温柔刺客君 2017-2-13 14:19:30
我只是路过打酱油的。
回复
697 0 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542

如何获得代码达人称号?

代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表