簡體   English   中英

雙擊創建動態 uiview

[英]Create dynamic uiview on double tap

各位程序員,你好,我想圍繞我的主 UIView 雙擊創建大小為 100X100 的動態 UIView(每次我雙擊不同的地方)。 我完全沒有任何想法。 所以不能提供任何代碼。 誰能幫忙??

  - (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];
}

試試這個代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM