在手势begin的,将UIImageView视图的transform记录下来,作为初始值。在手势的changed过程中,每一个changed时候获取的scale值都是和begin时的值的比率。在手势结束时,将scale的值也记录下来。
还有一个原因,这个pinch手势会多次执行,要知道从第一次缩放到最后退出,总的缩放比率是多大就得这样操作。
_lastPhotoScale = _lastPhotoScale*gesture.scale;
基本的数学概率,也很好理解。
这里缩放的仅仅是UIImageView视图,对于视图的image是没有任何改变的。因此,既然UIImageView视图的size很小了,但图片看上去还是非常清晰,图片的像素还是那么高呀。
如果需要将UIImage也做缩放的话,在UIImage中去实现。在UIImage真的缩小了以后,你发现,图片不清晰了。这是正常的,因为图片的体积真的小了。
- (void)scalePhoto:(UIPinchGestureRecognizer *)gesture { // GTMLoggerDebug(@"scale is %f",gesture.scale); if (gesture.state ==UIGestureRecognizerStateBegan) { currentTransform =_photoView.transform; } if (gesture.state ==UIGestureRecognizerStateChanged) { CGAffineTransform tr =CGAffineTransformScale(currentTransform, gesture.scale, gesture.scale); _photoView.transform = tr; _photoView.frame =CGRectMake(0,0, _photoView.frame.size.width,_photoView.frame.size.height); NSLog(@"ing:_lastPhotoScale is %f,scale is %f,frame is %@",_lastPhotoScale, gesture.scale,NSStringFromCGSize(_photoView.frame.size)); } // 当手指离开屏幕时,将lastscale设置为1.0 if ((gesture.state ==UIGestureRecognizerStateEnded) || (gesture.state ==UIGestureRecognizerStateCancelled)) { _lastPhotoScale =_lastPhotoScale*gesture.scale; NSLog(@"end:_lastPhotoScale is %f,scale is %f,frame is %@",_lastPhotoScale, gesture.scale,NSStringFromCGSize(_photoView.frame.size)); GTMLoggerDebug(@"_photoImage %p size is %@,_lastPhotoScale is %f",_photoImage, NSStringFromCGSize(_photoImage.size),_lastPhotoScale); GTMLoggerDebug(@"_photoView %p frame is %@",_photoView.image,NSStringFromCGRect(_photoView.frame)); } }
首先,下载第三方库,可以去官网下载,官网的地址我忘记了,但下面有一个我之前下的和我写的例子,其实官方的例子也写我们只是告诉大家用时需要把哪些代码复制出来就可以用了。
1、导入如下框架和第三方库
新浪微博分享例子下载:http://vdisk.weibo.com/s/BDn59yfnBUifA
下面是微博分享的代码里子:
-(IBAction)shareSina:(id)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:@"fenxiang" defaultContent:@"" image:[UIImage imageNamed:@"f"] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeSinaWeibo content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; }
这些代码例子可以在官方提供的例子中找到,官方例子下载地址:http://vdisk.weibo.com/s/BDn59yfnBUiAb
/** * @brief 分享到新浪微博 * * @param sender 事件对象 */ - (void)shareToSinaWeiboClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeSinaWeibo content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到腾讯微博 * * @param sender 事件对象 */ - (void)shareToTencentWeiboClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeTencentWeibo content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享给QQ好友 * * @param sender 事件对象 */ - (void)shareToQQFriendClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText title:@"ShareSDK" url:@"http://www.sharesdk.cn" musicFileUrl:nil extInfo:nil fileData:nil]; [ShareSDK shareContentWithType:ShareTypeQQ content:publishContent containerController:self statusBarTips:NO oneKeyShareList:nil shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到QQ空间 * * @param sender 事件对象 */ - (void)shareToQQSpaceClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [publishContent addQQSpaceUnitWithTitle:@"Hello QQ空间" url:@"http://www.sharesdk.cn" comment:INHERIT_VALUE summary:CONTENT image:INHERIT_VALUE imageQuality:INHERIT_VALUE type:INHERIT_VALUE playUrl:nil syncWeibo:nil]; [ShareSDK shareContentWithType:ShareTypeQQSpace content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享给微信好友 * * @param sender 事件对象 */ - (void)shareToWeixinSessionClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText title:@"ShareSDK" url:@"http://www.sharesdk.cn" musicFileUrl:nil extInfo:nil fileData:nil]; [ShareSDK shareContentWithType:ShareTypeWeixiSession content:publishContent containerController:self statusBarTips:NO oneKeyShareList:nil shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享给微信朋友圈 * * @param sender 事件对象 */ - (void)shareToWeixinTimelineClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeNews title:@"ShareSDK" url:@"http://www.baidu.com" musicFileUrl:nil extInfo:nil fileData:nil]; [ShareSDK shareContentWithType:ShareTypeWeixiTimeline content:publishContent containerController:self statusBarTips:YES autoAuth:YES convertUrl:YES shareViewOptions:nil result:nil]; } /** * @brief 分享到网易微博 * * @param sender 事件对象 */ - (void)shareTo163WeiboClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareType163Weibo content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到搜狐微博 * * @param sender 事件对象 */ - (void)shareToSohuWeiboClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeSohuWeibo content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到人人网 * * @param sender 事件对象 */ - (void)shareToRenRenClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeRenren content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到开心网 * * @param sender 事件对象 */ - (void)shareToKaiXinClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeKaixin content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到豆瓣我说 * * @param sender 事件对象 */ - (void)shareToDouBanClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeDouBan content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到Instapaper * * @param sender 事件对象 */ - (void)shareToInstapaperClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeInstapaper content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到Facebook * * @param sender 事件对象 */ - (void)shareToFacebookClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeFacebook content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 分享到Twitter * * @param sender Twitter */ - (void)shareToTwitterClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeTwitter content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:nil]; } /** * @brief 短信分享 * * @param sender 事件对象 */ - (void)shareBySMSClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeSMS content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:^(ShareType type, SSPublishContentState state, id<ISSStatusInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) { if (state == SSPublishContentStateSuccess) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"分享成功" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil]; [alertView show]; [alertView release]; } else if(state == SSPublishContentStateFail) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:error.errorDescription delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil]; [alertView show]; [alertView release]; } }]; } /** * @brief 邮件分享 * * @param sender 事件对象 */ - (void)shareByMailClickHandler:(UIButton *)sender { id<ISSPublishContent> publishContent = [ShareSDK publishContent:CONTENT defaultContent:@"" image:[UIImage imageNamed:IMAGE_NAME] imageQuality:0.8 mediaType:SSPublishContentMediaTypeText]; [ShareSDK shareContentWithType:ShareTypeMail content:publishContent containerController:self statusBarTips:YES oneKeyShareList:[NSArray defaultOneKeyShareList] shareViewStyle:ShareViewStyleDefault shareViewTitle:@"内容分享" result:^(ShareType type, SSPublishContentState state, id<ISSStatusInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) { if (state == SSPublishContentStateSuccess) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"分享成功" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil]; [alertView show]; [alertView release]; } else if(state == SSPublishContentStateFail) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:error.errorDescription delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil]; [alertView show]; [alertView release]; } }]; }
appWidget是显示的桌面上的小窗口程序,通过它可以达到用户与程序之间的交互。
下面我们来看下创建一个appWidget的步骤
一、首先在layout文件夹下创建一个appWidget的布局文件appwidgetlayout.xml, 在这里你可以添加一些需要在appWidget上显示的控件,如下:我只添加一个文本控件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/txtapp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" android:text="@string/bnt_name" > </TextView> </LinearLayout>
二、创建一个xml文件配置appWidget的显示属性,在res目录创建一个xml目录,在下面创建一个appwidget.xml文件,其实也可以把这个文件放在layout目录下,为了方便管理,我还是把它放在xml目录下,在这个目录下可以放置任何的xml文件。如下:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/appwidgetlayout" android:minHeight="72dp" android:minWidth="294dp" android:updatePeriodMillis="86400000" > </appwidget-provider>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.appwidgetdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.appwidgetdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.appwidgetdemo.appWidgetActivity" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" > </action> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget" /> </receiver> </application> </manifest>
重载以下几个函数,根据需要进行处理
1、public void onDeleted(Context context, int[] appWidgetIds) 删除一个AppWidget时调用
2、public void onDisabled(Context context) 最后一个appWidget被删除时调用
3、public void onEnabled(Context context) AppWidget的实例第一次被创建时调用
4、public void onReceive(Context context, Intent intent) 接收广播事件,可以接收系统的也可以接收自定义的
5、public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用
代码如下:
package com.example.appwidgetdemo; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; public class appWidgetActivity extends AppWidgetProvider { /** * 删除一个AppWidget时调用 */ @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub super.onDeleted(context, appWidgetIds); System.out.println("----------------onDeleted"); } /** * 最后一个appWidget被删除时调用 */ @Override public void onDisabled(Context context) { // TODO Auto-generated method stub super.onDisabled(context); System.out.println("----------------onDisabled"); } /** * AppWidget的实例第一次被创建时调用 */ @Override public void onEnabled(Context context) { // TODO Auto-generated method stub super.onEnabled(context); System.out.println("----------------onEnabled"); } /** * 接受广播事件 */ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub super.onReceive(context, intent); System.out.println("----------------onReceive"); } /** * 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用 */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { System.out.println("----------------onUpdate"); // TODO Auto-generated method stub } }
五、运行程序
程序运行后需要通过以下几点才能把appWidget小程序显示出来
1、如果是模拟器按鼠标左键不动会弹出如下窗口
2、点击“窗口小部件”会弹出如下窗口,显示出所有的appWidget窗口
3、选择我们刚才安装的部件,在主界面上显示如下:
至此我们第一个appWidget程序已经完成。
本例源码可以到以下链接下载:
点击打开链接