@class UIImageButton;
@protocol UIImageButtonDelegate <NSObject>
-(void)buttonImageViewClick:(UIImageButton *)imagebutton;
@end
@interface UIImageButton : UIImageView
@property(nonatomic,retain)id target;
@property(nonatomic,assign)SEL action;
//添加代理屬性
//面試題 代理屬性 爲什麼要聲明成assign
//防止循環引用 從而造成的內存泄漏
/*
假如 A是B的代理 B也是A的代理 如果是retain
[[A alloc]init]; 計數1
[[B alloc]init]; 計數1
A.delegate = B; B:2
B.delegate = A; A:2
[A release]; 1
[B release]; 1
這時 A B 都釋放不掉造成內存泄露
*/
{
[_target release];
[super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
self.target = target;
self.action = action;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.target performSelector:self.action withObject:nil];
if ([_delegate respondsToSelector:@selector(buttonImageViewClick:)]) {
[_delegate buttonImageViewClick:self];
}
//讓代理去調用代理方法
// [_delegate buttonImageViewClick];
// 這個類是個抽象類 其具體功能 交給子類去實習
//創建imageview 添加手勢
UIImageView *image = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
image.image = [UIImage imageNamed:@"5.jpg"];
[self.view addSubview:image];
image.userInteractionEnabled = YES;
[image release];
//輕拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//添加手勢到視圖上
[image addGestureRecognizer:tap];
[tap release];
//長按
//添加手勢的方法
//1.初始化手勢 添加手勢觸發調試的方法
//2.把手勢添加到視圖上
//3.釋放手勢
// UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
// //設置長按時間
// longpress.minimumPressDuration = 2.0;
// //添加到視圖上
// [image addGestureRecognizer:longpress];
// //釋放
// [longpress release];
//旋轉
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
// [image addGestureRecognizer:rotation];
// [rotation release];
//捏合
// UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchation:)];
// [image addGestureRecognizer:pinch];
// [pinch release];
//
// //平移手勢
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
// [image addGestureRecognizer:pan];
// [pan release];
//
//輕掃
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
//設置左右掃方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[image addGestureRecognizer:swipe];
[swipe release];
UIScreenEdgePanGestureRecognizer *edgepan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgepan:)];
//設置從那個邊緣掃
edgepan.edges = UIRectEdgeRight;
[image addGestureRecognizer:edgepan];
[edgepan release];
}
// 實現輕拍方法
-(void)tapAction:(UITapGestureRecognizer *)t
{
UIImageView *view = (UIImageView *)t.view;
view.image = [UIImage imageNamed:@"4.jpg"];
}
////實現長按方法
//-(void)longAction:(UILongPressGestureRecognizer *)l
//{
// //判斷一下狀態 長按 只需觸發一次
// if (l.state == UIGestureRecognizerStateBegan) {
// UIImageView *view = (UIImageView *)l.view;
// view.image = [UIImage imageNamed:@"3.jpg"];
// }
//}
//-(void)rotationAction:(UIRotationGestureRecognizer *)r
//{
// //形變屬性 transform
// //參數一 要改變形變屬性的視圖
// //參數二 根據弧度去創建
// r.view.transform = CGAffineTransformRotate(r.view.transform,r.rotation);
// //每次轉需要把旋轉的角度 重置爲0
// //因爲要接替上一次的角度 開始旋轉
// r.rotation = 0;
//}
//- (void)pinchation:(UIPinchGestureRecognizer *)p
//{
// //根據縮放的刻度(比例改變形變屬性)
//
// p.view.transform = CGAffineTransformScale(p.view.transform, p.scale, p.scale);
// p.scale = 1;
// NSLog(@"123");
//}
// 實現平移方法的點(相對於要平移的視圖)
//- (void)pan:(UIPanGestureRecognizer *)pan
//{
// CGPoint p = [pan translationInView:pan.view];
// // 根據這個點 改變形變屬性
// pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
// //重置這個點
// [pan setTranslation:CGPointMake(0, 0) inView:pan.view];
//}
-(void)swipe:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"123");
}
-(void)edgepan:(UIScreenEdgePanGestureRecognizer *)edgepan
{
NSLog(@"321");