当有人动摇iPhone时,我想做出反应.我并不特别在乎它们是如何摇晃它的,只是它在一瞬间大力挥手.有谁知道怎么检测到这个?
我需要添加一个可以刷新我的Android应用程序的摇动功能.
我发现的所有文档都涉及到实现SensorListener
,但Eclipse告诉我它已被弃用并建议SensorEventListener
.
谁有一个很好的指导我如何创建这个shake controller
?
我将以下代码添加到appDelegate.m中
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake )
{
// User was shaking the device. Post a notification named "shake".
[[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
- (void)shakeSuccess
{
// do something...
}
Run Code Online (Sandbox Code Playgroud)
然后我补充说:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// INIT THE BACKGROUND PATH STRING
[self refreshFields];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
***[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeSuccess) name:@"shake" …
Run Code Online (Sandbox Code Playgroud) 我正在制作一个Android应用程序,我有一个通向消息传递位置的按钮.在使用按钮的活动中,我检查是否有任何未读消息,如果是,我想对按钮做一些事情让用户知道有些东西未读.
我想让按钮sorta水平振动,每2或3秒3次震动.
我知道如何在后台运行一个每隔x毫秒做一次的线程.但我不知道该做什么是水平摇动3次.
有人能帮忙吗?
我正在考虑使用sin函数,对于动画,我可以使用sin函数的输出来获取上下的值,我可以设置按钮的水平位置......但这似乎太极端了,是还有更好的方法吗?
谢谢.
当按下后退按钮时,我需要将布尔值传递给intent,然后再返回intent.目标是设置布尔值并使用条件来防止在检测到onShake事件时多次启动新意图.我会使用SharedPreferences,但似乎它与我的onClick代码不相配,我不知道如何解决这个问题.任何建议,将不胜感激!
public class MyApp extends Activity {
private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorListener = new ShakeEventListener();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
public void onShake() {
// This code is launched multiple times on a vigorous
// shake of the device. I need to prevent this.
Intent myIntent = new Intent(MyApp.this, NextActivity.class);
MyApp.this.startActivity(myIntent);
}
});
}
@Override
protected …
Run Code Online (Sandbox Code Playgroud) Apple在iPhone SDK 3.0中宣布了Shake API.我找不到有关此新功能的任何信息.
谁知道如何使用它?任何例子,链接都会很好.
在摇动iPhone设备我想要一些功能被调用,我不知道如何识别摇,所以我尝试了一些链接,并尝试了这个代码
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if(event.type == UIEventSubtypeMotionShake) { NSLog(@"called"); [self.view setBackgroundColor:[UIColor greenColor]]; } } - (BOOL)canBecomeFirstResponder { return YES; }
但唉没有运气所以你能告诉我如何做同样的事情
感谢致敬
我是新的android和我正在寻找一种方法来点击我的按钮图像.到目前为止,我得到了这个,但它一直崩溃.或者,如果你点击它没有任何作用.我希望你们能帮助我.如果我忘了任何事情只是说出来.代码可能很乱.
我有一个动画文件夹.用shakeanim.xml
码:
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/chest"
android:background="@null"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"
android:text="100 Clicks" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
package com.example.egghatcher;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
ImageButton imageButton;
TextView clicksToGo;
Animation shake;
private int clicks = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shake = …
Run Code Online (Sandbox Code Playgroud) 我正在使用像这样的摇动api:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
[img stopAnimating];
}
}
Run Code Online (Sandbox Code Playgroud)
如何检测到摇晃已停止?
是否可以使用JavaScript收听iPhone(或任何带加速度计的手机)震动事件?你知道就像摇动iPhone来播放iTunes中的下一首歌一样,当iPhone被震动时我想在我的网站上调用JS功能.
shake ×10
android ×4
ios ×2
iphone ×2
objective-c ×2
animation ×1
button ×1
imagebutton ×1
java ×1
javascript ×1
xcode ×1
xml ×1