[英]How to detect touch on UIImageView and draw line?
我創建了一個ViewController,並在Custom類上選擇了myView。 在myView中,我已經進行了操作,當用戶單擊並拖動手指時,它將繪制一條直線,當再次拖動時,前一行消失,並創建新的。
我的代碼:
@implementation GestureView
{
CGPoint _originOfTouchPoint; // your fist touch detected in touchesBegan: method
CGPoint _currentFingerPositionPoint; // the position you have dragged your finger to
CGFloat _strokeWidth; // the width of the line you wish to draw
id _touchStartedObject; // the object(UIView) that the first touch was detected on
}
// If you use Interface Builder to design your interface, Objects in a nib file are reconstituted and then initialized using
// their initWithCoder: method
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
// Initialization code
_originOfTouchPoint = CGPointMake( 0.0, 0.0 );
_currentFingerPositionPoint = CGPointMake( 100.0, 100.0 );
_strokeWidth = 2.0;
}
return self;
}
/*
// Use initWithFrame if you are not loding the UIView from a nib file
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_originOfTouchPoint = CGPointMake( 0.0, 0.0 );
_currentFingerPositionPoint = CGPointMake( 100.0, 100.0 );
_strokeWidth = 2.0;
}
return self;
}
*/
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor( context, [UIColor blueColor].CGColor );
CGContextSetLineWidth( context, _strokeWidth );
// fisrt point of line
CGContextMoveToPoint( context, _originOfTouchPoint.x, _originOfTouchPoint.y );
// last point of line
CGContextAddLineToPoint( context, _currentFingerPositionPoint.x, _currentFingerPositionPoint.y );
// draw the line
CGContextStrokePath( context );
}
#pragma mark touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// get starting point and first view touched (if you need to send that view messages)
_originOfTouchPoint = [[touches anyObject] locationInView:self];
_touchStartedObject = [[touches anyObject] view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint movedToPoint = [[touches anyObject] locationInView:self];
// if moved to a new point redraw the line
if ( CGPointEqualToPoint( movedToPoint, _currentFingerPositionPoint ) == NO )
{
_currentFingerPositionPoint = movedToPoint;
// calls drawRect: method to show updated line
[self setNeedsDisplay];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// reset values
_originOfTouchPoint = CGPointZero;
_currentFingerPositionPoint = CGPointZero;
_touchStartedObject = nil;
}
@end
我在ViewController上添加了3個imageView,當用戶單擊任何imageView時,我想檢測觸摸,然后當他拖動手指時,該行開始從該imageView繪制。 我可以在屏幕上的任何地方畫線,但是我只想在單擊imageView並在另一個imageView結束時畫線
您可以使用-[UIView setUserInteractionEnabled:]
啟用觸摸,默認情況下在UIImageView
上為NO
關於從文檔中繪圖...
UIImageView類經過優化,可以將其圖像繪制到顯示器上。 UIImageView不會調用drawRect:一個子類。 如果您的子類需要自定義繪圖代碼,則建議您使用UIView作為基類。
試試這個:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//To get tag of touch view
int viewTag=[touch view].tag;
if ([[touch view] isKindOfClass:[UIImageView class]])
{
//Write your code...
}
}
並像這樣設置所有觸摸圖像UserInteractionEnabled:
[imageName setUserInteractionEnabled:YES];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.