Three20 中 Button 抖动 的 代码

本文介绍了一种使用UIView实现按钮摆动动画的方法。通过CGAffineTransform旋转矩阵来改变按钮的位置,达到摆动的效果,并利用UIView动画实现了动画过程。文章详细介绍了如何通过代码设置动画参数和动画结束后触发的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff; min-height: 13.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #00faa0} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #00b3f9} span.s1 {color: #e339a1} span.s2 {color: #ffffff} span.s3 {color: #00b3f9} span.s4 {color: #8c8af7} span.s5 {color: #ee8e62} span.s6 {color: #00faa0} </style>

- (void)wobble {
static BOOL wobblesLeft = NO;


if (_editing) {
CGFloat rotation = (50 * M_PI) / 180.0;
CGAffineTransform wobbleLeft = CGAffineTransformMakeRotation(rotation);
CGAffineTransform wobbleRight = CGAffineTransformMakeRotation(-rotation);


[UIView beginAnimations:nil context:nil];


NSInteger i = 0;
NSInteger nWobblyButtons = 0;
for (NSArray* buttonPage in _buttons) {
for (TTLauncherButton* button in buttonPage) {
if (button != _dragButton) {
++nWobblyButtons;
if (i % 2) {
button.transform = wobblesLeft ? wobbleRight : wobbleLeft;


} else {
button.transform = wobblesLeft ? wobbleLeft : wobbleRight;
}
}
++i;
}
}


if (nWobblyButtons >= 1) {
[UIView setAnimationDuration:kWobbleTime];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(wobble)];
wobblesLeft = !wobblesLeft;


} else {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(wobble) withObject:nil afterDelay:kWobbleTime];
}


[UIView commitAnimations];
}
}

//Small Fan /**************************************************************** * * 湖南创乐博智能科技有限公司 * As explained above, the amount of times you press the button should change the rotation speed of the fan. * Pressing it once will cause it to rotate slowly, * while pressing it three times will cause it to rotate quickly, and pressing it four times will cause it to stop. ****************************************************************/ // constants won't change. They're used here to // set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin const int motorIn1 = 9; const int motorIn2 = 10; int stat = 0; #define rank1 150 #define rank2 200 #define rank3 250 // Variables will change: int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin // the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers /******************************************************************************/ void setup() { //set theled,motors as OUTPUT,button as INPUT pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); pinMode(motorIn1,OUTPUT); pinMode(motorIn2,OUTPUT); Serial.begin(9600); } void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin); if (reading != lastButtonState)// If the button state is different from last time { lastDebounceTime = millis();// reset the debouncing timer } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; // Store the state of button in buttonState // only toggle the LED if the new button state is HIGH if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); //turn on the LED stat = stat + 1; if(stat >= 4)// When stat>=4, set it as 0. { stat = 0; } } else digitalWrite(ledPin, LOW); } } switch(stat) { case 1: clockwise(rank1);// When stat=1, set the rotate speed of the motor as rank1=150 break; case 2: clockwise(rank2);// When stat=2, set the rotate speed of the motor as rank1=200 break; case 3: clockwise(rank3);// When stat=3, set the rotate speed of the motor as rank1=250 break; default: clockwise(0);// else, set the rotate speed of the motor as rank1=150 } // save the reading. Next time through the loop, // it'll be the lastButtonState: lastButtonState = reading; } /***********************************************************/ void clockwise(int Speed)// { analogWrite(motorIn1,0); analogWrite(motorIn2,Speed); } /***********************************************************/ 代码如何实现的
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值