1. 新開一個專案
2. 在ViewController上放置三個Label
3. 將這些Label註冊到myTouchViewController.h(從Storyboard 的Label拖拉到檔案上)
#import <UIKit/UIKit.h>
@interface myTouchViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *startPointLabel;
@property (weak, nonatomic) IBOutlet UILabel *movePointLabel;
@property (weak, nonatomic) IBOutlet UILabel *endPointLabel;
@end
@interface myTouchViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *startPointLabel;
@property (weak, nonatomic) IBOutlet UILabel *movePointLabel;
@property (weak, nonatomic) IBOutlet UILabel *endPointLabel;
@end
4. 在myTouchViewController.m上加入所需程式碼如下,分別顯示手指滑動的起始點等。
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self.view];
startPointLabel.text = [NSString stringWithFormat:@"起點 x= %3.2f, y= %3.2f", point.x, point.y];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self.view];
movePointLabel.text = [NSString stringWithFormat:@"現在點 x= %3.2f, y= %3.2f", point.x, point.y];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self.view];
endPointLabel.text = [NSString stringWithFormat:@"終點 x = %3.2f, y= %3.2f", point.x , point.y];
}
5. 完成後顯示結果如下



沒有留言:
張貼留言