android手势如何平移控件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android手势如何平移控件相关的知识,希望对你有一定的参考价值。

解决方案1:
重写该控件的onTouch方法,move状态设置该view的margin或者在放手状态up中设置不需要手势监听吧,在该方法中判断,是down状态记录按下的位置,控件移动一般都是相对布局
解决方案2:
ontounchListener监听事件
解决方案3:
scroller调用scrollTo
知识点延伸阅读:
控件平移划过屏幕(Scroller简单使用)
MainActivity如下:
package cc.cn;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
/
Demo描述:
Scroller使用示例——让控件平移划过屏幕

注意事项:
1 在布局中将cc.cn.LinearLayoutSubClass的控件的宽度设置为"fill_parent"
便于观察滑动的效果
/
public class MainActivity extends Activity
private Button mButton;
private LinearLayoutSubClass mLinearLayoutSubClass;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();


private void init()
mLinearLayoutSubClass=(LinearLayoutSubClass) findViewById(R.id.linearLayoutSubClass);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
mLinearLayoutSubClass.beginScroll();

);



LinearLayoutSubClass如下:

package cc.cn;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.Scroller;
/**
* API注释:
*
* 1 //第一,二个参数起始位置;第三,四个滚动的偏移量;第五个参数持续时间
* startScroll(int startX, int startY, int dx, int dy, int duration)
*
* 2 //在startScroll()方法执行过程中即在duration时间内computeScrollOffset()
* 方法会一直返回true,但当动画执行完成后会返回返加false.
* computeScrollOffset()
*
* 3 当执行ontouch()或invalidate()或postInvalidate()均会调用该方法
* computeScroll()
*
*/
public class LinearLayoutSubClass extends LinearLayout
private Scroller mScroller;
private boolean flag=true;

public LinearLayoutSubClass(Context context)
super(context);


public LinearLayoutSubClass(Context context, AttributeSet attrs)
super(context, attrs);
//也可采用该构造方法传入一个interpolator
//mScroller=new Scroller(context, interpolator);
mScroller=new Scroller(context);


@Override
public void computeScroll()
super.computeScroll();
if(mScroller.computeScrollOffset())
scrollTo(mScroller.getCurrX(), 0);
//使其再次调用computeScroll()直至滑动结束,即不满足if条件
postInvalidate();



public void beginScroll()
if (flag)
mScroller.startScroll(0, 0, -2500, 0, 2500);
flag = false;
else
mScroller.startScroll(0, 0, 0, 0, 1500);
flag = true;

//调用invalidate();使其调用computeScroll()
invalidate();


参考技术A 不需要手势监听吧,控件移动一般都是相对布局,重写该控件的onTouch方法,在该方法中判断,是down状态记录按下的位置,move状态设置该view的margin或者在放手状态up中设置。本回答被提问者和网友采纳 参考技术B ontounchListener监听事件 参考技术C scroller调用scrollTo

如何使集合视图响应其自身视图之外的平移手势

【中文标题】如何使集合视图响应其自身视图之外的平移手势【英文标题】:How to make a collectionview respond to pan gestures outside of it's own view 【发布时间】:2016-09-20 13:39:14 【问题描述】:

我的UIViewController 中有一个UICollectionView,我希望它能够响应UICollectionView 内外的手势。默认情况下,UICollectionView 仅响应其自己的view 内的手势,但我怎样才能让它响应其view 之外的滑动?

谢谢。

【问题讨论】:

在您的viewController 中使用touches:began 【参考方案1】:

我写了一个视图子类来完成这个:

#import <UIKit/UIKit.h>

@interface TouchForwardingView : UIView

@property (nonatomic, weak) IBOutlet UIResponder *forwardingTarget;

- (instancetype)initWithForwardingTarget:(UIResponder *)forwardingTarget;


@end

#import "TouchForwardingView.h"

@implementation TouchForwardingView

- (instancetype)initWithForwardingTarget:(UIResponder *)forwardingTarget

    self = [super init];
    if (self)
    
        self.forwardingTarget = forwardingTarget;
    

    return self;


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    [super touchesBegan:touches withEvent:event];
    [self.forwardingTarget touchesBegan:touches withEvent:event];


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

    [super touchesEnded:touches withEvent:event];
    [self.forwardingTarget touchesEnded:touches withEvent:event];


- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

    [super touchesCancelled:touches withEvent:event];
    [self.forwardingTarget touchesCancelled:touches withEvent:event];


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

    [super touchesMoved:touches withEvent:event];
    [self.forwardingTarget touchesMoved:touches withEvent:event];


@end

在界面生成器中,将包含视图的子视图设置为 TouchForwardingView,然后将集合视图分配给 forwardingTarget 属性。

【讨论】:

【参考方案2】:

Nailer 的答案的 Swift 版本,这会将在 viewcontroller 上完成的所有手势转发到 collectionview

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) 
    collectionView.touchesBegan(touches, withEvent: event)

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) 
    collectionView.touchesEnded(touches, withEvent: event)

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) 
    collectionView.touchesCancelled(touches, withEvent: event)

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) 
    collectionView.touchesMoved(touches, withEvent: event)

【讨论】:

【参考方案3】:

Steven B 对 Swift 4 的回答 :)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) 
    collectionView.touchesBegan(touches, with: event)

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) 
    collectionView.touchesEnded(touches, with: event)

override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) 
    collectionView.touchesCancelled(touches!, with: event)

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 
    collectionView.touchesMoved(touches, with: event)

【讨论】:

以上是关于android手势如何平移控件的主要内容,如果未能解决你的问题,请参考以下文章

Android 高仿微信头像截取 打造不一样的自己定义控件

android如何对viewpager里面的图片进行缩放

android控件随手势旋转

Android自定义控件之可平移、缩放、旋转图片控件

Android自己定义控件实战——仿多看阅读平移翻页

Android自定义控件篇 图片进行平移,缩放,旋转