標籤:藍懿教育 劉國斌 ios 培訓
//開始觸摸
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// NSSet集合 和數組類似 都是用來裝多個對象的 但是區別是set無序
//擷取set集合中任意對象
[touches anyObject];
//把set集合轉成array數組
NSArray *arr = touches.allObjects;
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self.view];
NSLog(@"x=%f y=%f",p.x,p.y);
NSLog(@"%@",NSStringFromCGPoint(p));
//便利所有圖片
for (UIImageView *iv in self.imageViews) {
//判斷觸碰的是哪一個圖片
if (CGRectContainsPoint(iv.frame, p)) {
//將觸碰的圖片變為屬性,讓移動觸摸位置方法等方法可以調用
self.dragView = iv;
break;
}
}
}
//移動觸摸位置
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self.view];
//讓觸摸圖片的中心位置等於觸摸的位置
self.dragView.center = p;
}
//結束觸摸
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self.view];
NSLog(@"end%@",NSStringFromCGPoint(p));
//結束觸摸時,解除touch的座標與圖片的綁定
self.dragView = nil;
}
//當發生意外中斷時如有電話進來
-(void)touchesCancelled:(NSSt *)touches withEvent:(UIEvent *)event{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self.view];
NSLog(@"Cancell%@",NSStringFromCGPoint(p));
}
將控制項從原來父視圖self.view移動到新的父視圖self.leftView,座標位移值修正方法
//當父視圖發生改變的時候需要把相對於原來父視圖的座標轉換成相對於新父視圖的座標
CGPoint oldCenter = self.dragView.center;
CGPoint newCenter = [self.view convertPoint:oldCenter toView:self.leftView];
[self.leftView addSubview:self.dragView];
self.dragView.center = newCenter;
記錄添加到視圖中控制項的個數
通過視圖子視圖數組中中項目的個數
self.rightLabel.text = [NSString stringWithFormat:@"%d",self.rightView.subviews.count];
讓超出視圖邊緣的部分不顯示
storyboard中選中視圖屬性中Drawing中的Clip SubSubviews勾選
藍懿教育九月二十八日記錄