【问题标题】:Create dynamic uiview on double tap双击创建动态uiview
【发布时间】:2018-07-15 03:47:59
【问题描述】:

各位程序员,大家好。, 我想在我双击主 UIView 周围创建大小为 100X100 的动态 UIView(每次我双击不同的地方)。我对此完全没有任何想法。所以不能为它提供任何代码。谁能帮忙??

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: ios objective-c iphone uiviewcontroller


【解决方案1】:
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
   CGPoint currentPos = [myTouch locationInView: self.view];
   NSLog(@"Point in myView: (%f,%f)", currentPos.x, currentPos.y);

  UIView*hover = [[UIView alloc] initWithFrame:CGRectMake(currentPos.x - 50, currentPos.y - 50 , 100, 100)];

  hover.backgroundColor = [UIColor grayColor];

 [self.view addSubview:hover];
}

【讨论】:

  • 谢谢 SH_Khan,对不起。我做到了,但我想创建 uiview,我双击不在同一个位置,
  • 非常感谢@Sh_Khan。
【解决方案2】:

试试这个代码

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addDynamicView:)];
self.view.userInteractionEnabled = TRUE;
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];

添加自定义视图

- (void)addDynamicView:(UITapGestureRecognizer*)aGesture{
    /** add view with specified frame **/
    CGRect rect = CGRectMake(10, 10, 100, 100);
    UIView *lView = [[UIView alloc] initWithFrame:rect];
    lView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:lView];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多