关于移动中的button的响应问题
来源:互联网 发布:知乎如何使用邮箱注册 编辑:程序博客网 时间:2024/05/22 00:09
在ios中 如果动画效果中包含的button的改变 ,比如移动,这时候button的点击事件是不会响应的,如果想要时间响应 有两种办法
1.使用定时器微分的方法,比较消耗内存,这里才用0.01s做为时间间隔
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#define which 2
#define number 4
@implementation ViewController
{
UIView *primaryView;
UIView *secondaryView;
UIButton *_btnMove;
NSTimer *_timer;
}
@synthesize displayingPrimary;
- (void) viewDidLoad {
[superviewDidLoad];
[selfcreateMoveButton];
}
/**
* 移动动画且button移动中还可以交互
*/
- (void)createMoveButton
{
_btnMove = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[_btnMoveaddTarget:selfaction:@selector(btnMoveDidSelect)forControlEvents:UIControlEventTouchUpInside];
_btnMove.frame =CGRectMake(0,0,80, 40);
[_btnMovesetTitle:@"touch me!"forState:UIControlStateNormal];
[self.view addSubview:_btnMove];
_timer = [NSTimerscheduledTimerWithTimeInterval:0.01ftarget:selfselector:@selector(buttonMoved)userInfo:nilrepeats:YES];
}
- (void)buttonMoved
{
if(_btnMove.frame.origin.x !=250)
{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.01f];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseOut];
[_btnMovesetFrame:CGRectMake(_btnMove.frame.origin.x+1,_btnMove.frame.origin.y,_btnMove.frame.size.width,_btnMove.frame.size.height)];
[UIViewcommitAnimations];
}else{
[_timer invalidate];//关闭定时器
}
}
- (void)btnMoveDidSelect
{
NSLog(@"tap!");
}
2.使用ios动画过程中的 图层触碰测试
自定义一个Myview
#import "MyView.h"
#import <QuartzCore/QuartzCore.h>
@implementation MyView {
IBOutlet UIView* v;
}
- (void) awakeFromNib {
[superawakeFromNib];
UITapGestureRecognizer* t = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)];
[vaddGestureRecognizer:t];
t.cancelsTouchesInView =NO;
}
- (void) tap: (UIGestureRecognizer*) g {
NSLog(@"tap! (gesture recognizer)");
}
- (UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//这里对触碰测试做判断
// v 是参与动画的子视图
CALayer* lay = [v.layerpresentationLayer]; //获取视图的展示层
CALayer* hitLayer = [lay hitTest: point];
if (hitLayer == lay || hitLayer.superlayer == lay)
return v;
UIView* hitView = [superhitTest:pointwithEvent:event];
if (hitView == v)
return self;
return hitView;
}
然后再viewcontroller的xib里 将self.view 设置为 自定义的Myview
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
.m文件
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation ViewController {
IBOutlet UIButton *button;
CGPoint oldButtonCenter;
}
- (void)viewDidLoad
{
[superviewDidLoad];
oldButtonCenter = button.center;
}
- (IBAction)tapme:(id)sender {
NSLog(@"tap! (the button's action method)");
}
- (IBAction)start:(id)sender {
NSLog(@"you tapped Start");
CGPoint goal = CGPointMake(100,400);
button.center =oldButtonCenter;
UIViewAnimationOptions opt =UIViewAnimationOptionAllowUserInteraction;
[UIViewanimateWithDuration:10delay:0options:opt animations:^{
button.center = goal;
} completion:^(BOOL f) {
}];
}
这样就可以在动画过程中实现点击事件了
- 关于移动中的button的响应问题
- 关于Android的Button响应页面跳转问题
- 关于移动端响应式全屏背景图显示的问题
- 关于在collectionView Header 中的button不可点击的问题
- 关于Android中ExpandableListView子项是Button控件而单击事件没有响应的问题
- 关于可移动Button的定制
- android listviewListview中的button点击事件或者是onitemclick不响应的问题
- 关于Button点击两次才响应事件相关问题
- 关于button的小问题
- 关于UGUI中Button,InputField等不响应的解决方法
- Button 的响应方式
- android listview 和button 共存的事件响应问题。
- android LinearLayout嵌套 button时onClickListener不响应的问题
- JS中button控件click事件无法响应的问题
- 关于把button加到UIImageView上后button无法响应单击事件的方法
- 1个关于button移动
- Button控制ViewPager滑动,解决在小米4上Button无响应的问题
- 关于UITapGestureRecognizer与button的问题
- DirectDraw系统集成
- DirectShow-13 Seeking
- 回调函数
- DirectShow - 14 DirectShow的线程和临界区
- 【iOS开发】---- 强大的UI修改工具 UIAppearance
- 关于移动中的button的响应问题
- Cocos2dx 小技巧(一)预定义文件路径
- 小说名词解释:种田派
- 时间管理推荐书目
- BitmapFactory.Options.inSampleSize 的用法
- GTD的好处
- 动漫追踪--反叛的鲁鲁修
- 动漫追踪--空之境界
- 推荐读书论坛--栖息谷