当前位置: 编程技术>综合
本页文章导读:
▪为View添加手势 #import <UIKit/UIKit.h>
@interface TestGestureRecognizerViewController : UIViewController {
UIView *aView;
}
@property(nonatomic,retain)IBOutlet UIView *aView;
@end
///////////////////////////////////////////////////////////////////////////.........
▪Django笔记01 1 安装django
2 创建项目(project)
3 创建应用(app)
4 将app应用注册到settings中
5 配置urlconfig (urls.py)
1 url(/blog_article/regpattern, 全字符串/index.html)
2 url(/blog_article/regpattern, 处理方法字符串/index.html)---> patterns()第一个参数:前缀字.........
▪java nio SocketChannel 服务器端与多客户端 信息交互(聊天功能) 服务器端:
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.ch.........
[1]为View添加手势
来源: 互联网 发布时间: 2013-11-05
#import <UIKit/UIKit.h> @interface TestGestureRecognizerViewController : UIViewController { UIView *aView; } @property(nonatomic,retain)IBOutlet UIView *aView; @end /////////////////////////////////////////////////////////////////////////////////////////////// #import "TestGestureRecognizerViewController.h" #import <QuartzCore/QuartzCore.h>//导入框架 @implementation TestGestureRecognizerViewController @synthesize aView; -(void)handelPan:(UIPanGestureRecognizer*)gestureRecognizer{ //获取平移手势对象在self.view的位置点,并将这个点作为self.aView的center,这样就实现了拖动的效果 CGPoint curPoint = [gestureRecognizer locationInView:self.view]; [self.aView setCenter:curPoint]; } -(void)handelTap:(UITapGestureRecognizer *)gestureRecognizer{ NSLog(@"%s",__FUNCTION__); [NSRunLoop cancelPreviousPerformRequestsWithTarget:self];//双击事件取消延时 } //单击方法 -(void)handelSingleTap:(UITapGestureRecognizer*)gestureRecognizer{ NSLog(@"%s",__FUNCTION__); [self performSelector:@selector(singleTap:) withObject:nil afterDelay:0.2]; } -(void)singleTap:(id)sender{ NSLog(@"%s",__FUNCTION__); } - (void)viewDidLoad { [super viewDidLoad]; //创建一个平移手势对象,该对象可以调用handelPan:方法 UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handelPan:)]; [self.aView addGestureRecognizer:panGes]; [panGes release]; //创建一个点击手势对象,该对象可以调用handelTap:方法 UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelTap:)]; [self.view addGestureRecognizer:tapGes]; [tapGes release]; [tapGes setNumberOfTouchesRequired:1];//触摸点个数 [tapGes setNumberOfTapsRequired:2];//点击次数 //创建一个点击手势对象,该对象可以调用handelSingleTap:方法 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelSingleTap:)]; [self.view addGestureRecognizer:singleTap]; [singleTap release]; [singleTap setNumberOfTouchesRequired:1];//触摸点个数 [singleTap setNumberOfTapsRequired:1];//点击次数 } - (void)dealloc { [super dealloc]; } @end
作者:zzzili 发表于2013-1-5 14:21:37 原文链接
阅读:42 评论:0 查看评论
[2]Django笔记01
来源: 互联网 发布时间: 2013-11-05
1 安装django
2 创建项目(project)
3 创建应用(app)
4 将app应用注册到settings中
1 url(/blog_article/regpattern, 全字符串/index.html)
2 url(/blog_article/regpattern, 处理方法字符串/index.html)---> patterns()第一个参数:前缀字符串
3 patterns 叠加
6 urlconfig和视图处理方法映射关系
HttpResponse对象的作用
方法的第一个参数:req --> 请求对象
project目录下运行:
python manager.py runserver
127.0.0.1:8000
python manager.py runserver 0.0.0.0:port
在app下,创建模板目录: blog/templates
在模板目录下 ,创建模板文件
1 导入相关对象:loader, Context
from django.template import loader, Context
2 加载模板文件
t = loader.get_template(模板文件名)
3 创建Context对象
c = Context
4 模板对象渲染context对象 ---> html
views def : Context({'变量名':变量值, ...... })
模板文件 {{变量名}}
python(views):数值, 字符串, bool,
tuple, list, dict, object
list, tuple使用索引访问,不能负索引
dict,使用key访问
class:
调用属性
调用方法(不能有参数)
变量 dot 引用方式优先级:
key, 属性, 方法, 索引
作者:forgetbook 发表于2013-1-5 14:06:26 原文链接
阅读:55 评论:0 查看评论
[3]java nio SocketChannel 服务器端与多客户端 信息交互(聊天功能)
服务器端:
客户端,可运行启动多个:
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; import java.util.Set; public class NIOSServer { private int port = 8888; //解码buffer private Charset cs = Charset.forName("gbk"); /*接受数据缓冲区*/ private static ByteBuffer sBuffer = ByteBuffer.allocate(1024); /*发送数据缓冲区*/ private static ByteBuffer rBuffer = ByteBuffer.allocate(1024); /*映射客户端channel */ private Map<String, SocketChannel> clientsMap = new HashMap<String, SocketChannel>(); private static Selector selector; public NIOSServer(int port){ this.port = port; try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() throws IOException{ /* *启动服务器端,配置为非阻塞,绑定端口,注册accept事件 *ACCEPT事件:当服务端收到客户端连接请求时,触发该事件 */ ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); ServerSocket serverSocket = serverSocketChannel.socket(); serverSocket.bind(new InetSocketAddress(port)); selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); System.out.println("server start on port:"+port); } /** * 服务器端轮询监听,select方法会一直阻塞直到有相关事件发生或超时 */ private void listen(){ while (true) { try { selector.select();//返回值为本次触发的事件数 Set<SelectionKey> selectionKeys = selector.selectedKeys(); for(SelectionKey key : selectionKeys){ handle(key); } selectionKeys.clear();//清除处理过的事件 } catch (Exception e) { e.printStackTrace(); break; } } } /** * 处理不同的事件 */ private void handle(SelectionKey selectionKey) throws IOException { ServerSocketChannel server = null; SocketChannel client = null; String receiveText=null; int count=0; if (selectionKey.isAcceptable()) { /* * 客户端请求连接事件 * serversocket为该客户端建立socket连接,将此socket注册READ事件,监听客户端输入 * READ事件:当客户端发来数据,并已被服务器控制线程正确读取时,触发该事件 */ server = (ServerSocketChannel) selectionKey.channel(); client = server.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); } else if (selectionKey.isReadable()) { /* * READ事件,收到客户端发送数据,读取数据后继续注册监听客户端 */ client = (SocketChannel) selectionKey.channel(); rBuffer.clear(); count = client.read(rBuffer); if (count > 0) { rBuffer.flip(); receiveText = String.valueOf(cs.decode(rBuffer).array()); System.out.println(client.toString()+":"+receiveText); dispatch(client, receiveText); client = (SocketChannel) selectionKey.channel(); client.register(selector, SelectionKey.OP_READ); } } } /** * 把当前客户端信息 推送到其他客户端 */ private void dispatch(SocketChannel client,String info) throws IOException{ Socket s = client.socket(); String name = "["+s.getInetAddress().toString().substring(1)+":"+Integer.toHexString(client.hashCode())+"]"; if(!clientsMap.isEmpty()){ for(Map.Entry<String, SocketChannel> entry : clientsMap.entrySet()){ SocketChannel temp = entry.getValue(); if(!client.equals(temp)){ sBuffer.clear(); sBuffer.put((name+":"+info).getBytes()); sBuffer.flip(); //输出到通道 temp.write(sBuffer); } } } clientsMap.put(name, client); } public static void main(String[] args) throws IOException { NIOSServer server = new NIOSServer(7777); server.listen(); } }
客户端,可运行启动多个:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Date; import java.util.Set; public class NIOClient { /*发送数据缓冲区*/ private static ByteBuffer sBuffer = ByteBuffer.allocate(1024); /*接受数据缓冲区*/ private static ByteBuffer rBuffer = ByteBuffer.allocate(1024); /*服务器端地址*/ private InetSocketAddress SERVER; private static Selector selector; private static SocketChannel client; private static String receiveText; private static String sendText; private static int count=0; public NIOClient(int port){ SERVER = new InetSocketAddress("localhost", port); init(); } public void init(){ try { /* * 客户端向服务器端发起建立连接请求 */ SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); selector = Selector.open(); socketChannel.register(selector, SelectionKey.OP_CONNECT); socketChannel.connect(SERVER); /* * 轮询监听客户端上注册事件的发生 */ while (true) { selector.select(); Set<SelectionKey> keySet = selector.selectedKeys(); for(final SelectionKey key : keySet){ handle(key); }; keySet.clear(); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { NIOClient client = new NIOClient(7777); } private void handle(SelectionKey selectionKey) throws IOException{ if (selectionKey.isConnectable()) { /* * 连接建立事件,已成功连接至服务器 */ client = (SocketChannel) selectionKey.channel(); if (client.isConnectionPending()) { client.finishConnect(); System.out.println("connect success !"); sBuffer.clear(); sBuffer.put((new Date().toLocaleString()+" connected!").getBytes()); sBuffer.flip(); client.write(sBuffer);//发送信息至服务器 /* * 启动线程一直监听客户端输入,有信心输入则发往服务器端 * 因为输入流是阻塞的,所以单独线程监听 */ new Thread(){ @Override public void run() { while(true){ try { sBuffer.clear(); InputStreamReader input = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(input); sendText = br.readLine(); /* * 未注册WRITE事件,因为大部分时间channel都是可以写的 */ sBuffer.put(sendText.getBytes()); sBuffer.flip(); client.write(sBuffer); } catch (IOException e) { e.printStackTrace(); break; } } }; }.start(); } //注册读事件 client.register(selector, SelectionKey.OP_READ); } else if (selectionKey.isReadable()) { /* * 读事件触发 * 有从服务器端发送过来的信息,读取输出到屏幕上后,继续注册读事件 * 监听服务器端发送信息 */ client = (SocketChannel) selectionKey.channel(); rBuffer.clear(); count=client.read(rBuffer); if(count>0){ receiveText = new String( rBuffer.array(),0,count); System.out.println(receiveText); client = (SocketChannel) selectionKey.channel(); client.register(selector, SelectionKey.OP_READ); } } } }
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—
最新技术文章: