zoukankan      html  css  js  c++  java
  • 创建一个可任意拖放的控件

    看到有个app上面有个浮动的可以随意拖动的漂浮控件

    想了下可以用UIButton, button设置要显示的图片, 然后通过UIPanGestureRecognizer来实现调整位置

    上代码:

     1 - (void)viewDidLoad {
     2     [super viewDidLoad];
     3     
     4     //初始化button
     5     _button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
     6     _button.backgroundColor     = [UIColor redColor];
     7     _button.layer.cornerRadius  = 50;
     8     _button.layer.masksToBounds = YES;
     9     _button.center              = self.view.center;
    10     
    11     [self.view addSubview:_button];
    12     
    13     UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    14     [self.button addGestureRecognizer:panGesture];
    15 }
    16 
    17 - (void)handlePanGesture: (UIPanGestureRecognizer *)recognizer {
    18     
    19     CGPoint translation    = [recognizer translationInView:self.view];
    20     recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
    21                                          recognizer.view.center.y + translation.y);
    22     [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    23 }
  • 相关阅读:
    242
    241
    240
    「音乐」(G)I-DLE—화(火花) (HWAA)
    「音乐」Red Velvet—Psycho
    「音乐」Taylor Swift—the lakes (bonus track)
    「笔记」DP简单笔记
    「游记」CSP2020-S2游记
    「音乐」BLACKPINK-Ice Cream (with Selena Gomez)
    matlab 2021a linux 版本下载
  • 原文地址:https://www.cnblogs.com/zhouxihi/p/6013768.html
Copyright © 2011-2022 走看看