方法一:
在viewDidLoad时为UIView添加操作
UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release];
响应操作绑定的事件
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{ //UISwipeGestureRecognizerDirectionLeft UISwipeGestureRecognizerDirectionRight UISwipeGestureRecognizerDirectionUp UISwipeGestureRecognizerDirectionDown if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft ) { NSLog(@"Left"); } }
方法二:
#define HORIZ_SWIPE_DRAG_MIN 12 #define VERT_SWIPE_DRAG_MAX 4 CGPoint startTouchPosition; NSString *dirString ; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = touches.anyObject; startTouchPosition = [touch locationInView:self.view]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = touches.anyObject; CGPoint currentTouchPosition = [touch locationInView:self.view]; if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= HORIZ_SWIPE_DRAG_MIN && fabsf(startTouchPosition.y - currentTouchPosition.y) <= VERT_SWIPE_DRAG_MAX) { // Horizontal Swipe if (startTouchPosition.x < currentTouchPosition.x) { NSLog(@"from left"); dirString = @"left"; } else{ NSLog(@"from right"); dirString = @"right";} } else if (fabsf(startTouchPosition.y - currentTouchPosition.y) >= HORIZ_SWIPE_DRAG_MIN && fabsf(startTouchPosition.x - currentTouchPosition.x) <= VERT_SWIPE_DRAG_MAX) { // Vertical Swipe if (startTouchPosition.y < currentTouchPosition.y) { NSLog(@"from bottom"); dirString = @"bottom";} else { NSLog(@"from top"); dirString = @"top";} } else { //dirString = @""; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if (dirString == @"right") { NSLog(@"event right"); }else if (dirString == @"left") { NSLog(@"event left"); } }
1.去除默认的点击选中时的颜色
(1)设置列表layout的backgroudcolor属性就OK了。
(2)android:focusable="false" android:focusableInTouchMode="false"
android:cacheColorHint="#00000000" android:listSelector="#00000000"
2.替换listview 默认的点击选中时的颜色
设置listivew的listSelector属性就可以了。
1.首先定义一个boolean[] listItemState用来保存列表项选中的状态!
2.在onItemClick中
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
System.out.println("position:" + position + ",count:"
+ listAdapter.getCount());
if (listItemState[position]) {
listItemState[position] = false;
view.setBackgroundResource(0);//不设置颜色
} else {
listItemState[position] = true;
view.setBackgroundResource(R.drawable.betresult_list_focus);
}
}
3.在重写的Adapter 的getView方法中,写入类似这样的语句。
if (listItemState[position]) {
view.setSelected(true);
view.setPressed(true);
// view.setBackgroundColor(Color.BLUE);
view.setBackgroundResource(R.drawable.betresult_list_focus);
} else {
view.setSelected(false);
view.setPressed(false);
// view.setBackgroundColor(Color.BLUE);
}
如此,选中的列表项可以高亮显示!