【发布时间】:2018-07-15 03:47:59
【问题描述】:
各位程序员,大家好。, 我想在我双击主 UIView 周围创建大小为 100X100 的动态 UIView(每次我双击不同的地方)。我对此完全没有任何想法。所以不能为它提供任何代码。谁能帮忙??
【问题讨论】:
-
到目前为止你尝试了什么?
标签: ios objective-c iphone uiviewcontroller
各位程序员,大家好。, 我想在我双击主 UIView 周围创建大小为 100X100 的动态 UIView(每次我双击不同的地方)。我对此完全没有任何想法。所以不能为它提供任何代码。谁能帮忙??
【问题讨论】:
标签: ios objective-c iphone uiviewcontroller
- (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];
}
【讨论】: